Jump to content

Exceptions: Difference between revisions

No edit summary
(→‎[[Standard ML]]: Added Forth)
Line 137:
//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]]==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.