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

m (→‎{{header|PL/I}}: It's incomplete rather than incorrect.)
Line 286:
=={{header|D}}==
First exception will be caught and message will be displayed, second will be caught by default exception handler.
<lang D>import std.stdio: writeln;
 
class U0 : Exception {
{{libheader|tango}}
class U0 : Exception { this() { super("U0 error message"); } }
}
 
class U1 : Exception {
<lang D>module test;
class U1 : Exception { this() { super("U1 error message"); } }
}
 
void foo() {
import tango.io.Stdout;
foreach (i; 0 .. 2) {
 
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 writeln("Exception U0 caught").newline;
}
}
Line 309 ⟶ 307:
 
void bar(int i) { baz(i); }
 
void baz(int i) {
{
if (!i) throw new U0;
else throw new U1U0;
{ else
throw new U1;
}
 
void main() { foo(); }</lang>
foo();
 
}</lang>
Result:
test<pre>testb.U1: U1 error message
<pre>
Exception U0 caught</pre>
test.U1: U1 error message
</pre>
 
If you have tango stack-trace, stack-trace will be print after second message.
 
=={{header|Eiffel}}==
Anonymous user