Exceptions: Difference between revisions
Content added Content deleted
No edit summary |
(→[[Standard ML]]: Added Forth) |
||
Line 137: | Line 137: | ||
//This code is always executed after exiting the try block |
//This code is always executed after exiting the try block |
||
} |
} |
||
==[[Forth]]== |
|||
Forth's exception mechanism is, like most things in Forth, very simple but powerful. |
|||
CATCH captures the data and return stack pointers, then exectutes an execution token. |
|||
THROW conditionally throws a value up to the most recent CATCH |
|||
===Throw Exceptions=== |
|||
: f ( -- ) 1 throw ." f " ; |
|||
: g ( -- ) 0 throw ." g " ; |
|||
===Catch Exceptions=== |
|||
: report ( n -- ) ?dup if ." caught " . else ." no throw" then ; |
|||
: test ( -- ) |
|||
['] f catch report |
|||
['] g catch report ; |
|||
test example. (Output shown in bold) |
|||
cr test |
|||
'''caught 1 g no throw ok''' |
|||
==[[Standard ML]]== |
==[[Standard ML]]== |