fOOrth/docs/snippets/exception.foorth
2015-08-11 11:49:21 -04:00

57 lines
1 KiB
Text

(Exceptions in action)
(Phase One - Living Dangerously)
: danger
0 10 do
50 i 5 - / . space
loop ;
(Phase Two - Living Tediously)
: tedium
0 10 do
50 i 5 -
dup 0<> if
/ . space
else
drop drop ."oops "
then
loop ;
(Phase Three - Living Exceptionally)
: safety
0 10 do
try
50 i 5 - / . space
catch
."oops "
end
loop ;
(Phase Four - Cleaning Up After Yourself)
: cleanup
"temp.txt" OutStream .create val: out_file
."File opened" cr
try
."Danger comes next." cr
1 0 / out_file .
."Danger has passed." cr
finally
out_file .close
."File closed" cr
end ;
(Phase Five - All Together Now)
: last_example
"temp.txt" OutStream .create val: out_file
."File opened" cr
try
."Danger comes next." cr
1 0 / out_file .
."Danger has passed." cr
catch
."Error detected." cr
finally
out_file .close
."File closed" cr
end ;