try statement catch statement
If no error occurs, the result of evaluating the try statement. If an error occurs, the result of the catch statement.
This statement catches any errors that may occur while evaluating the try statement code. If no error occurs, then try catch will finish without ever evaluating the catch statement. If an error does occur, try catch will evaluate the catch statement code immediately and the error condition will be cleared. This is usually used to protect a running program from a piece of unpredictable code, such as an event handler. If the error is not caught it will be propagated to the top-level error handler, causing the interpreter to go into an interactive debugging session.
The following code:
#!/usr/cogent/bin/gamma try { 2 + nil; } catch { princ("Error:\n", _error_stack_, "\n"); }
Will give these results:
Error: ((trap_error #0=(+ 2 nil) (princ Error: _error_stack_ )) #0#)
The following piece of code will run an event loop and protect against an unpredictable event.
while(t) { try (next_event()) catch (print_trapped_error()); } function print_trapped_error () { princ("Error:\n", _error_stack_, "\n"); princ("Clearing error condition and continuing.\n"); }
Copyright © 1995-2010 by Cogent Real-Time Systems, Inc. All rights reserved.