Exceptions/Catch an exception thrown in a nested call: Difference between revisions
Content added Content deleted
(Fixed and updated D entry) |
|||
Line 509: | Line 509: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{incorrect|D|Function names wrong. foo() <-> baz().}} |
|||
First exception will be caught and message will be displayed, second will be caught by default exception handler. |
First exception will be caught and message will be displayed, second will be caught by default exception handler. |
||
<lang d> |
<lang d>class U0 : Exception { |
||
⚫ | |||
class U0 : Exception { |
|||
⚫ | |||
} |
} |
||
class U1 : Exception { |
class U1 : Exception { |
||
this() nothrow { super("U1 error message"); } |
this() @safe pure nothrow { super("U1 error message"); } |
||
} |
} |
||
void foo( |
void foo() { |
||
import std.stdio; |
|||
⚫ | |||
else |
|||
throw new U0; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
void baz() { |
|||
⚫ | |||
try { |
try { |
||
i.bar; |
|||
} catch (U0 |
} catch (U0) { |
||
"Function foo caught exception U0".writeln; |
|||
} |
} |
||
} |
} |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
void baz(in int i) @safe pure { |
|||
⚫ | |||
} |
} |
||
void main() { |
void main() { |
||
foo; |
|||
}</lang> |
}</lang> |
||
{{output}} |
|||
Result: |
|||
<pre>test.U1 |
<pre>test.U1(at)test.d(8): U1 error message |
||
---------------- |
---------------- |
||
\test.d(20): pure void test.bar(int) |
\test.d(20): pure void test.bar(int) |