Jump to content

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

→‎{{header|REXX}}: added the REXX language. -- ~~~~
(→‎{{header|REXX}}: added the REXX language. -- ~~~~)
Line 1,597:
2: tryCatch(bar()) at file.r#4
1: foo()
</pre>
 
=={{header|REXX}}==
While the REXX language doesn't have a ''throw'' capability ''pe se'', it does have the ability to catch exceptions (by label).
<br>This type of exception handling (in REXX) has its limitation (the label is local to the program, not external subroutines).
<lang rexx>/*REXX program to create two exceptions & demonstrate how to handle them*/
call foo /*invoke the FOO function. */
say 'mainline program is done.' /*indicate that Elroy was here. */
exit /*stick a fork in it, we're done.*/
 
/*──────────────────────────────────FOO function────────────────────────*/
foo: call bar; call bar /*invoke BAR function twice. */
return 0 /*return a zero to invoker. */
U0: say 'exception U0 caught in FOO' /*handle the U0 exception. */
return /*stick a fork in it, we're done.*/
 
/*──────────────────────────────────BAR function────────────────────────*/
bar: call baz /*have BAR invoke BAZ function. */
return 0 /*return a zero to invoker. */
 
/*──────────────────────────────────BAZ function────────────────────────*/
baz: if symbol('BAZ#')=='LIT' then baz#=0 /*initialize BAZ invocation#*/
baz# = baz#+1 /*bump the BAZ invocation # by 1.*/
if baz#==1 then signal U0 /*if first invocation, raise U0 */
if baz#==2 then signal U1 /* " second " " U1 */
return 0 /*return a zero to invoker. */
U0: return -1 /*handle exception if not caught.*/
U1: return -1 /* " " " " " */</lang>
'''output'''
<pre style="overflow:scroll">
exception U0 caught in FOO
mainline program is done.
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.