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

 
(9 intermediate revisions by 6 users not shown)
Line 23:
F baz(i)
I i == 0
X.throw U0()
E
X.throw U1()
 
F bar(i)
Line 220:
 
=={{header|Amazing Hopper}}==
<p>Hopper has a basic "try/catch" handling, and must be handled manually. Only one exception will be liftedraised.</p>
<p>VERSION 1: </p>
<syntaxhighlight lang="bennuGDc">
#include <jambo.h>
 
Line 237:
Subrutines
 
/*
This "Try" is not considered nested
*/
Define ' Foo '
Gosub ' Bar '
Line 269 ⟶ 266:
</pre>
<p>VERSION 2: </p>
<syntaxhighlight lang="bennuGDc">
#include <jambo.h>
 
Line 1,176 ⟶ 1,173:
foo()
{
for(i := 0,; i < 2,; i := i + 1)
{
try
Line 2,073 ⟶ 2,070:
 
There is no explicit try block. A catch implicitly wraps the instructions preceding it within a block into a try block.
 
Prior to 0.7, you would use .err instead of _err for an implicit exception variable.
 
<syntaxhighlight lang="langur">val .U0 = h{"msg": "U0"}
val .U1 = h{"msg": "U1"}
 
val .baz = ffn(.i) { throw if(.i==0: .U0; .U1) }
val .bar = ffn(.i) { .baz(.i) }
 
val .foo = fimpure fn() {
for .i in [0, 1] {
.bar(.i)
catch {
if _err["'msg"] == .U0["'msg"] {
writeln "caught .U0 in .foo()"
} else {
Line 2,095 ⟶ 2,090:
}
 
.foo()</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 3,098 ⟶ 3,094:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func baz(i) { die "U#{i}" };
func bar(i) { baz(i) };
 
func foo {
[0, 1].each { |i|
try { bar(i) }
catch { |_, msg|
msg ~~ /^U0/  ? say "Function foo() caught exception U0"
 : die msg; # re-raise the exception
};
}
}
 
foo();</syntaxhighlight>
{{out}}
<pre>
Line 3,446 ⟶ 3,442:
 
We can use that approach here, re-throwing the second (uncaught) exception so that it terminates the script.
<syntaxhighlight lang="ecmascriptwren">var U0 = "U0"
var U1 = "U1"
 
885

edits