Exceptions/Catch an exception thrown in a nested call: Difference between revisions
Content added Content deleted
m (added whitespace and highlighting to the task's preamble.) |
|||
Line 1,926: | Line 1,926: | ||
%**-------------------------------------------------------------- |
%**-------------------------------------------------------------- |
||
</pre> |
</pre> |
||
=={{header|PARI/GP}}== |
|||
<lang parigp>call = 0; |
|||
U0() = error("x = ", 1, " should not happen!"); |
|||
U1() = error("x = ", 2, " should not happen!"); |
|||
baz(x) = if(x==1, U0(), x==2, U1());x; |
|||
bar() = baz(call++); |
|||
foo() = if(!call, iferr(bar(), E, printf("Caught exception, call=%d",call)), bar())</lang> |
|||
Output 1. call to foo():<pre>Caught exception, call=1</pre> |
|||
Output 2. call to foo():<pre> *** at top-level: foo() |
|||
*** ^----- |
|||
*** in function foo: ...ception, call=%d",call)),bar()) |
|||
*** ^------ |
|||
*** in function bar: baz(call++) |
|||
*** ^----------- |
|||
*** in function baz: if(x==1,U0(),x==2,U1());x |
|||
*** ^------- |
|||
*** in function U1: error("x = ",2," sho |
|||
*** ^-------------------- |
|||
*** user error: x = 2 should not happen! |
|||
</pre> |
|||
Output 3. call to foo():<pre>3</pre> |
|||
=={{header|Pascal}}== |
=={{header|Pascal}}== |