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

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 218: Line 218:
'''par''' clause, then all parallel the threads are terminated and the
'''par''' clause, then all parallel the threads are terminated and the
program continues in the parent thread. <!-- example needed -->
program continues in the parent thread. <!-- example needed -->

=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
=== True exceptions ===
=== True exceptions ===
Line 484: Line 485:
*** Error: U1 Bartender Error. Bartender kicked customer out of bar Baz.
*** Error: U1 Bartender Error. Bartender kicked customer out of bar Baz.
</pre>
</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#}}==
=={{header|C sharp|C#}}==
Line 573: Line 532:
</pre>
</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}}==
=={{header|Clojure}}==
Line 777: Line 777:


The uncaught exception shows a Windows Error Report dialog.
The uncaught exception shows a Windows Error Report dialog.



=={{header|DWScript}}==
=={{header|DWScript}}==
Line 877: Line 876:
👓 error: U1 not catched
👓 error: U1 not catched
</lang>
</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}}==
=={{header|Eiffel}}==
Line 949: Line 992:
Developer exception:</pre>
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}}==
=={{header|Elena}}==
ELENA 5.0 :
ELENA 5.0 :
Line 1,821: Line 1,821:
<pre>caught exception U0
<pre>caught exception U0
uncaught exception: U1</pre>
uncaught exception: U1</pre>

=={{header|jq}}==
=={{header|jq}}==
{{works with|jq|>1.4}}
{{works with|jq|>1.4}}
Line 2,383: Line 2,384:
</pre>
</pre>
Output 3. call to foo():<pre>3</pre>
Output 3. call to foo():<pre>3</pre>



=={{header|Pascal}}==
=={{header|Pascal}}==
Line 2,414: Line 2,414:
...propagated at exceptionsnested.pl line 5.
...propagated at exceptionsnested.pl line 5.
</pre>
</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}}==
=={{header|Phix}}==
Line 2,688: Line 2,664:
. . failed 1
. . failed 1
</lang>
</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}}==
=={{header|REXX}}==