Exceptions: Difference between revisions

Content deleted Content added
m Changed over to headers
m Re-alphabetized.
Line 98: Line 98:
}
}
}
}

=={{header|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, restoring the stack pointers.

===Throw Exceptions===
: f ( -- ) 1 throw ." f " ; \ will throw a "1"
: g ( -- ) 0 throw ." g " ; \ does not throw

===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'''

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
}
}

=={{header|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, restoring the stack pointers.

===Throw Exceptions===
: f ( -- ) 1 throw ." f " ; \ will throw a "1"
: g ( -- ) 0 throw ." g " ; \ does not throw

===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'''

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}}==
===Define Exceptions===
exception MyException;
exception MyDataException of int; (* can be any first-class type, not just int *)

===Throw Exceptions===
fun f() = raise MyException;
fun g() = raise MyDataException 22;

===Catch Exceptions===
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}}==
===Define Exceptions===
exception MyException;
exception MyDataException of int; (* can be any first-class type, not just int *)

===Throw Exceptions===
fun f() = raise MyException;
fun g() = raise MyDataException 22;

===Catch Exceptions===
val x = f() handle MyException => 22;
val y = f() handle MyDataException x => x;