Jump to content

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

m
→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations, changed some literals.
(→‎{{header|TXR}}: Update. The ! convention for opening commands is deprecated, supported in backward-compatibility mode only. Substitute new output.)
m (→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations, changed some literals.)
Line 2,177:
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 &nbsp;
(the label is known global to the program, but not to external subroutines).
<lang rexx>/*REXX program to createcreates two exceptions &and demonstratedemonstrates how to handle (catch) them. */
call foo /*invoke the FOO function. (below). */
say 'The REXX mainline program ishas donecompleted.' /*indicate that Elroy was here. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────FOO function────────────────────────*/
foo: call bar; call bar /*invoke BAR function twice. */
return 0 /*return a zero to the invoker. */
/*the 1st U0 in REXX program is used.*/
U0: say 'exception U0 caught in FOO' /*handle the U0 exception. */
returnU0: -2 say 'exception U0 caught in FOO' /*handle the U0 exception. /*return to the invoker. */
return -2 /*return to the invoker. */
/*──────────────────────────────────BAR function────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
bar: call baz /*have BAR invoke BAZ function. */
bar: call baz return 0 /*return a zero to invoker. /*have BAR function invoke BAZ function*/
return 0 /*return a zero to the invoker. */
/*──────────────────────────────────BAZ function────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
baz: if symbol('BAZ#')=='LIT' then baz#=0 /*initialize BAZ invocation#*/
baz: if symbol('BAZ#')=='LIT' then baz# = baz#+1 0 /*bumpinitialize the first BAZ invocation # by 1.*/
if baz# == baz#+1 then signal U0 /*ifbump firstthe BAZ invocation, raisenumber U0by 1. */
if baz#==21 then signal U1U0 /* " second " /*if first invocation, then " raise U1U0 */
returnif 0baz#==2 then signal U1 /* " second " " /*return a " 0 (zero) toU1 invoker.*/
return 0 /*return a [↓] this0 U0(zero) subto isthe ignoredinvoker.*/
U0: return -1 /*handle exception[↓] this U0 ifsubroutine notis caughtignored.*/
U1U0: return -1 /* " /*handle exception "if not caught. " " " */</lang>
U1: return -1 /* " " " " " */</lang>
{{out}}
'''output'''
<pre>
exception U0 caught in FOO
The REXX mainline program ishas donecompleted.
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.