Jump to content

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

(added java)
Line 47:
Procedure Foo caught exception U0
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}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.