Next: , Previous: Execution Tokens Tutorial, Up: Tutorial


3.30 Exceptions

throw ( n -- ) causes an exception unless n is zero.

     100 throw .s
     0 throw .s

catch ( ... xt -- ... n ) behaves similar to execute, but it catches exceptions and pushes the number of the exception on the stack (or 0, if the xt executed without exception). If there was an exception, the stacks have the same depth as when entering catch:

     .s
     3 0 ' / catch .s
     3 2 ' / catch .s
Assignment: Try the same with execute instead of catch.

Throw always jumps to the dynamically next enclosing catch, even if it has to leave several call levels to achieve this:

     : foo 100 throw ;
     : foo1 foo ." after foo" ;
     : bar ['] foo1 catch ;
     bar .

It is often important to restore a value upon leaving a definition, even if the definition is left through an exception. You can ensure this like this:

     : ...
        save-x
        ['] word-changing-x catch ( ... n )
        restore-x
        ( ... n ) throw ;

However, this is still not safe against, e.g., the user pressing Ctrl-C when execution is between the catch and restore-x.

Gforth provides an alternative exception handling syntax that is safe against such cases: try ... restore ... endtry. If the code between try and endtry has an exception, the stack depths are restored, the exception number is pushed on the stack, and the execution continues right after restore.

The safer equivalent to the restoration code above is

     : ...
       save-x
       try
         word-changing-x 0
       restore
         restore-x
       endtry
       throw ;

Reference: Exception Handling.