|
|
Line 98: |
Line 98: |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
⚫ |
|
|
⚫ |
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, restoring the stack pointers. |
|
|
|
|
⚫ |
|
|
⚫ |
: f ( -- ) 1 throw ." f " ; \ will throw a "1" |
|
⚫ |
: g ( -- ) 0 throw ." g " ; \ does not throw |
|
|
|
|
⚫ |
|
|
⚫ |
: report ( n -- ) ?dup if ." caught " . else ." no throw" then ; |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
test example. (Output shown in bold) |
|
⚫ |
|
|
⚫ |
'''caught 1 g no throw ok''' |
|
|
|
|
⚫ |
Note that CATCH only restores the stack pointers, not the stack values, so any values that were changed during the execution of the token will have undefined values. In practice, this means writing code to clean up the stack, like this: |
|
⚫ |
10 ['] myfun catch if drop then |
|
|
|
|
|
|
|
|
=={{header|Java}}== |
|
=={{header|Java}}== |
Line 134: |
Line 156: |
|
//This code is always executed after exiting the try block |
|
//This code is always executed after exiting the try block |
|
} |
|
} |
|
|
|
⚫ |
|
|
⚫ |
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, restoring the stack pointers. |
|
|
|
|
⚫ |
|
|
⚫ |
: f ( -- ) 1 throw ." f " ; \ will throw a "1" |
|
⚫ |
: g ( -- ) 0 throw ." g " ; \ does not throw |
|
|
|
|
⚫ |
|
|
⚫ |
: report ( n -- ) ?dup if ." caught " . else ." no throw" then ; |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
test example. (Output shown in bold) |
|
⚫ |
|
|
⚫ |
'''caught 1 g no throw ok''' |
|
|
|
|
⚫ |
Note that CATCH only restores the stack pointers, not the stack values, so any values that were changed during the execution of the token will have undefined values. In practice, this means writing code to clean up the stack, like this: |
|
⚫ |
10 ['] myfun catch if drop then |
|
|
|
|
⚫ |
=={{header|Standard ML}}== |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
exception MyDataException of int; (* can be any first-class type, not just int *) |
|
|
|
|
⚫ |
|
|
⚫ |
fun f() = raise MyException; |
|
⚫ |
fun g() = raise MyDataException 22; |
|
|
|
|
⚫ |
|
|
⚫ |
val x = f() handle MyException => 22; |
|
⚫ |
val y = f() handle MyDataException x => x; |
|
|
|
|
|
|
=={{header|JavaScript}}== |
|
=={{header|JavaScript}}== |
Line 188: |
Line 176: |
|
eventSetup = true; |
|
eventSetup = true; |
|
} |
|
} |
|
|
|
|
|
|
|
|
=={{header|Perl}}== |
|
=={{header|Perl}}== |
Line 283: |
Line 272: |
|
custom_error = |
|
custom_error = |
|
if 'oops' print |
|
if 'oops' print |
|
|
|
|
⚫ |
=={{header|Standard ML}}== |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
exception MyDataException of int; (* can be any first-class type, not just int *) |
|
|
|
|
⚫ |
|
|
⚫ |
fun f() = raise MyException; |
|
⚫ |
fun g() = raise MyDataException 22; |
|
|
|
|
⚫ |
|
|
⚫ |
val x = f() handle MyException => 22; |
|
⚫ |
val y = f() handle MyDataException x => x; |