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

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 218:
'''par''' clause, then all parallel the threads are terminated and the
program continues in the parent thread. <!-- example needed -->
 
=={{header|AutoHotkey}}==
=== True exceptions ===
Line 484 ⟶ 485:
*** Error: U1 Bartender Error. Bartender kicked customer out of bar Baz.
</pre>
 
=={{header|C++}}==
First exception will be caught and message will be displayed,
second will be caught by the default exception handler,
which as required by the C++ Standard, will call terminate(),
aborting the task, typically with an error message.
 
<lang cpp>#include <iostream>
class U0 {};
class U1 {};
void baz(int i)
{
if (!i) throw U0();
else throw U1();
}
void bar(int i) { baz(i); }
void foo()
{
for (int i = 0; i < 2; i++)
{
try {
bar(i);
} catch(U0 e) {
std::cout<< "Exception U0 caught\n";
}
}
}
int main() {
foo();
std::cout<< "Should never get here!\n";
return 0;
}</lang>
 
Result:
<pre>
Exception U0 caught
This application has requested the Runtime to terminate it in an unusual way.
</pre>
The exact behavior for an uncaught exception is implementation-defined.
 
=={{header|C sharp|C#}}==
Line 573 ⟶ 532:
</pre>
 
=={{header|C++}}==
First exception will be caught and message will be displayed,
second will be caught by the default exception handler,
which as required by the C++ Standard, will call terminate(),
aborting the task, typically with an error message.
 
<lang cpp>#include <iostream>
class U0 {};
class U1 {};
void baz(int i)
{
if (!i) throw U0();
else throw U1();
}
void bar(int i) { baz(i); }
void foo()
{
for (int i = 0; i < 2; i++)
{
try {
bar(i);
} catch(U0 e) {
std::cout<< "Exception U0 caught\n";
}
}
}
int main() {
foo();
std::cout<< "Should never get here!\n";
return 0;
}</lang>
 
Result:
<pre>
Exception U0 caught
This application has requested the Runtime to terminate it in an unusual way.
</pre>
The exact behavior for an uncaught exception is implementation-defined.
 
=={{header|Clojure}}==
Line 777:
 
The uncaught exception shows a Windows Error Report dialog.
 
 
=={{header|DWScript}}==
Line 877 ⟶ 876:
👓 error: U1 not catched
</lang>
 
=={{header|EGL}}==
{{incorrect|EGL|calls to bar() from foo should be equivalent. Second call can't catch anything.}}
<lang EGL>record U0 type Exception
end
 
record U1 type Exception
end
 
program Exceptions
 
function main()
foo();
end
 
function foo()
try
bar();
onException(ex U0)
SysLib.writeStdout("Caught a U0 with message: '" :: ex.message :: "'");
end
bar();
end
 
function bar()
baz();
end
 
firstBazCall boolean = true;
function baz()
if(firstBazCall)
firstBazCall = false;
throw new U0{message = "This is the U0 exception"};
else
throw new U1{message = "This is the U1 exception"};
end
end
end</lang>
 
{{out}}
<pre>
Caught a U0 with message: 'This is the U0 exception'
This is the U1 exception
</pre>
 
=={{header|Eiffel}}==
Line 949 ⟶ 992:
Developer exception:</pre>
 
=={{header|EGL}}==
{{incorrect|EGL|calls to bar() from foo should be equivalent. Second call can't catch anything.}}
<lang EGL>record U0 type Exception
end
 
record U1 type Exception
end
 
program Exceptions
 
function main()
foo();
end
 
function foo()
try
bar();
onException(ex U0)
SysLib.writeStdout("Caught a U0 with message: '" :: ex.message :: "'");
end
bar();
end
 
function bar()
baz();
end
 
firstBazCall boolean = true;
function baz()
if(firstBazCall)
firstBazCall = false;
throw new U0{message = "This is the U0 exception"};
else
throw new U1{message = "This is the U1 exception"};
end
end
end</lang>
 
{{out}}
<pre>
Caught a U0 with message: 'This is the U0 exception'
This is the U1 exception
</pre>
=={{header|Elena}}==
ELENA 5.0 :
Line 1,821:
<pre>caught exception U0
uncaught exception: U1</pre>
 
=={{header|jq}}==
{{works with|jq|>1.4}}
Line 2,383 ⟶ 2,384:
</pre>
Output 3. call to foo():<pre>3</pre>
 
 
=={{header|Pascal}}==
Line 2,414:
...propagated at exceptionsnested.pl line 5.
</pre>
 
=={{header|Perl 6}}==
{{trans|Perl}}
<lang perl6>sub foo() {
for 0..1 -> $i {
bar $i;
CATCH {
when /U0/ { say "Function foo caught exception U0" }
}
}
}
 
sub bar($i) { baz $i }
 
sub baz($i) { die "U$i" }
 
foo;</lang>
{{out}}
<pre>Function foo caught exception U0
U1
in sub baz at catch:12
in sub bar at catch:10
in sub foo at catch:4
in block at catch:14</pre>
 
=={{header|Phix}}==
Line 2,688 ⟶ 2,664:
. . failed 1
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{trans|Perl}}
<lang perl6>sub foo() {
for 0..1 -> $i {
bar $i;
CATCH {
when /U0/ { say "Function foo caught exception U0" }
}
}
}
 
sub bar($i) { baz $i }
 
sub baz($i) { die "U$i" }
 
foo;</lang>
{{out}}
<pre>Function foo caught exception U0
U1
in sub baz at catch:12
in sub bar at catch:10
in sub foo at catch:4
in block at catch:14</pre>
 
=={{header|REXX}}==
10,333

edits