The catch statement is used to catch and recuperate from errors. The catch statement has the following syntax:
catch error code, error code, ..body-statementwithhandler-statement
The keyword any can be substituded for the list of errors to to catch all errors. The handler statement is optional, and does not need to be defined.
If an error listed in the error list is thrown inside the body-statement, the interpreter will catch the error and start executing handler-statement rather than aborting the method. After the handler is done, the interpreter continues executing after the end of the catch statement, as if body-statement had completed with no errors.
Inside the handler statement, the function error() can be used to retrieve the error code which triggered the handler and the function traceback() can be used to retrieve the propagated error stack. The function rethrow() is used to continue propagating an error. The function throw() can be used at any time to throw an error.
Here is an example of how a catch statement could intelligently handle a ~methodnf error thrown from the list_method() function:
catch ~methodnf code = list_method(method_name); with .tell("There is no method named " + tostr(method_name) + ".");
Note that critical expressions inside body-statement will override the behavior of the catch statement.
Simple | Conditional | Error-Handling | Looping Statements