Exceptions/Catch an exception thrown in a nested call: Difference between revisions
Exceptions/Catch an exception thrown in a nested call (view source)
Revision as of 19:04, 1 May 2024
, 16 days ago→{{header|langur}}
Langurmonkey (talk | contribs) |
|||
(12 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 218:
'''par''' clause, then all parallel the threads are terminated and the
program continues in the parent thread. <!-- example needed -->
=={{header|Amazing Hopper}}==
<p>Hopper has a basic "try/catch" handling, and must be handled manually. Only one exception will be raised.</p>
<p>VERSION 1: </p>
<syntaxhighlight lang="c">
#include <jambo.h>
Main
e=0, se=""
Try
Gosub 'Foo'
Catch (e)
Get msg exception, and Move to 'se'
Printnl ("+-MAIN-FOO CALL Error: ",e, " : ", se )
Finish
End
Subrutines
Define ' Foo '
Gosub ' Bar '
Return
Define ' Bar '
Set '0', Gosub ' Biz '
Set '1', Gosub ' Biz '
Return
Define ' Biz, x '
a=0, b=0
If ( x )
Let ' b:=Sqrt(-1) '
Nan( a ) do{ Raise (1000,"\n+----Func BIZ: NaN!") }
Else
#( a=log(-1) + log(-1) )
Nan( a ) do{ Raise (1001,"\n+----Func BIZ: NaN!") }
End If
Printnl ' "a = ", a, " b = ", b '
Return
</syntaxhighlight>
{{out}}
<pre>
+-MAIN-FOO CALL Error: 1001 :
+----Func BIZ: NaN!
</pre>
<p>VERSION 2: </p>
<syntaxhighlight lang="c">
#include <jambo.h>
Main
e=0, se=""
Try
Gosub 'Foo'
Catch (e)
Get msg exception, and Move to 'se'
Printnl ("+-MAIN Error: ",e, " : ", se )
Finish
End
Subrutines
/*
This "Try" is not considered nested, then, it is necessary
to capture the error and raise the error
*/
Define ' Foo '
Try
Gosub ' Bar '
Catch (e)
Get msg exception, and Move to 'se'
Free try // absolutly nessesary in this chase!
Raise (e, Cat ("\n+--FUNC FOO: ", se) )
Finish
Return
Define ' Bar '
Try
Set '0', Gosub ' Biz '
Set '1', Gosub ' Biz '
Catch(e)
Get msg exception, and Move to 'se'
Free try // absolutly nessesary in this chase!
Raise (e, Cat ("\n+---FUNC BAR: ", se) )
Finish
Return
Define ' Biz, x '
a=0, b=0
If ( x )
Let ' b:=Sqrt(-1) '
Nan( a ) do{ Raise (1000,"\n+----Func BIZ: NaN!") }
Else
#( a=log(-1) + log(-1) )
Nan( a ) do{ Raise (1001,"\n+----Func BIZ: NaN!") }
End If
Printnl ' "a = ", a, " b = ", b '
Return
</syntaxhighlight>
{{out}}
<pre>
+-MAIN Error: 1001 :
+--FUNC FOO:
+---FUNC BAR:
+----Func BIZ: NaN!
</pre>
=={{header|APL}}==
Line 1,027 ⟶ 1,137:
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">import extensions;
class U0 : Exception
{
}
class U1 : Exception
{
}
singleton Exceptions
{
}
}
Line 1,084 ⟶ 1,194:
<pre>
U0 Caught
U1 exception
Call stack:
sandbox'$private'Exceptions.baz[1]:sandbox.l(30)
sandbox'$private'Exceptions.foo[1]:sandbox.l(40)
sandbox'program.function:#invoke:sandbox.l(52)
system'$private'entry.function:#invoke:app.l(5)
system'$private'entrySymbol#sym:app.l(23)
Aborted:ffffffff
</pre>
Line 1,961 ⟶ 2,070:
There is no explicit try block. A catch implicitly wraps the instructions preceding it within a block into a try block.
<syntaxhighlight lang="langur">val .U0 = h{"msg": "U0"}
val .U1 = h{"msg": "U1"}
val .baz =
val .bar =
val .foo =
for .i in [0, 1] {
.bar(.i)
catch {
if _err
writeln "caught .U0 in .foo()"
} else {
Line 1,983 ⟶ 2,090:
}
.foo()
</syntaxhighlight>
{{out}}
Line 2,986 ⟶ 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 ~~ /^U0/
}
}
}
foo()
{{out}}
<pre>
Line 3,334 ⟶ 3,442:
We can use that approach here, re-throwing the second (uncaught) exception so that it terminates the script.
<syntaxhighlight lang="
var U1 = "U1"
|