Flow-control structures: Difference between revisions
Content added Content deleted
m (→iterate: corrected a wrong word. -- ~~~~) |
(→{{header|REXX}}: added an example of raising a condition (division by zero). -- ~~~~) |
||
Line 1,645: | Line 1,645: | ||
===exceptions=== |
===exceptions=== |
||
(See the '''signal''' statement below.) |
(See the '''signal''' statement and '''raising conditions''' below.) |
||
===exit=== |
===exit=== |
||
Line 1,727: | Line 1,727: | ||
end /*k*/ |
end /*k*/ |
||
say 'sum=' sum</lang> |
say 'sum=' sum</lang> |
||
===raising conditions=== |
|||
(REXX) conditions can be raised by causing some kind of "failure" or triggering event (such as division by zero). |
|||
A '''signal''' statement must have been issued previous to the event being triggered to enable trapping. |
|||
(Also, see the '''signal''' statement below.) |
|||
<lang rexx>... |
|||
signal on syntax |
|||
... |
|||
y=4 - 4 |
|||
x=66 |
|||
say x/y /*divide x by y.*/ |
|||
say "yup, that's a divide by zero, by gum." |
|||
exit |
|||
syntax: say |
|||
/* We can now possibly do some repair work , but most people trap */ |
|||
/* the condition, display where it happened, the REXX sourceline */ |
|||
/* (the actual REXX statement), which condition was triggered, */ |
|||
/* display any other pertinent REXX variables, which line in the */ |
|||
/* REXX program, and then (usually) exit with some kind of error */ |
|||
/* message and error code indicator. */ |
|||
/* Note: the "name" of the REXX program isn't quite accurate, */ |
|||
/* rather, it is the name that was invoked (called by), which may */ |
|||
/* be different name than the actual program being executed. */ |
|||
say '──────────────────────error!─────────────────────────' |
|||
say 'that division (above) will cause control to get here.' |
|||
parse source . . fid . |
|||
say; say 'REXX raised a SYNTAX error in program:' fid |
|||
say; say 'it occurred on line' sigl |
|||
say; say 'the REXX statement is:' /*put it on separate line.*/ |
|||
say sourceline(sigl) |
|||
say; say 'which code:' condition('C') "error" |
|||
say; say 'error code:' condition('D') |
|||
say; say "Moral: don't do that." |
|||
exit 13</lang> |
|||
'''output''' |
|||
<pre> |
|||
──────────────────────error!───────────────────────── |
|||
that division (above) will cause control to get here. |
|||
REXX raised a SYNTAX error in program: D:\OOPSsay.REX |
|||
it occurred on line 6 |
|||
the REXX statement is: |
|||
say x/y /*divide x by y.*/ |
|||
which code: SYNTAX error |
|||
error code: Error 42.3: Arithmetic overflow; divisor must not be zero |
|||
Moral: don't do that. |
|||
</pre> |
|||
===return=== |
===return=== |
||
Line 1,779: | Line 1,837: | ||
* when a command executed returns an error return code [other than 0 (zero)]. |
* when a command executed returns an error return code [other than 0 (zero)]. |
||
* when a command executed indicates a failure. |
* when a command executed indicates a failure. |
||
(Also, see '''raising conditions''' above.) |
|||
<lang rexx>signal on error |
<lang rexx>signal on error |
||
signal on failure |
signal on failure |