Exceptions/Catch an exception thrown in a nested call: Difference between revisions

Content added Content deleted
(added java)
Line 47: Line 47:
Procedure Foo caught exception U0
Procedure Foo caught exception U0
Exception U1 passed through foo</pre>
Exception U1 passed through foo</pre>

=={{header|D}}==
First exception will be caught and message will be displayed, second will be caught by default exception handler.

<lang D>
module test;

import tango.io.Stdout;

class U0 : Exception { this() { super("U0 error message"); } }
class U1 : Exception { this() { super("U1 error message"); } }

void foo()
{
for (int i = 0; i < 2; i++)
{
try {
bar(i);
} catch(U0 e) {
Stdout ("Exception U0 caught").newline;
}
}
}

void bar(int i) { baz(i); }
void baz(int i)
{
if (!i) throw new U0;
else throw new U1;
}

void main() { foo(); }
</lang>

Result:
<lang D>
Exception U0 caught
test.U1: U1 error message
</lang>

If you have tango stack-trace, stack-trace will be print after second message.


=={{header|Java}}==
=={{header|Java}}==