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

Added Wren
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
(Added Wren)
Line 3,189:
at Program.Foo() in Program.vb:line 17
at Program.Main() in Program.vb:line 11</pre>
 
=={{header|Wren}}==
As explained in the [[Exceptions#Wren]] task, Wren doesn't have exceptions as such but we can simulate them by trying to run code which may cause an error in a fiber and then capturing any error that does occur.
 
We can use that approach here, re-throwing the second (uncaught) exception so that it terminates the script.
<lang ecmascript>var U0 = "U0"
var U1 = "U1"
 
var bazCalled = 0
 
var baz = Fn.new {
bazCalled = bazCalled + 1
Fiber.abort( (bazCalled == 1) ? U0 : U1 )
}
 
var bar = Fn.new {
baz.call()
}
 
var foo = Fn.new {
for (i in 1..2) {
var f = Fiber.new { bar.call() }
f.try()
var err = f.error
if (err == U0) {
System.print("Caught exception %(err)")
} else if (err == U1) {
Fiber.abort("Uncaught exception %(err) rethrown") // re-throw
}
}
}
 
foo.call()</lang>
 
{{out}}
<pre>
Caught exception U0
Uncaught exception U1 rethrown
[./exceptions_nested line 23] in new(_) block argument
[./exceptions_nested line 28] in (script)
</pre>
 
=={{header|zkl}}==
9,476

edits