Next: , Previous: The unwind_protect Statement, Up: Statements


12.9 The try Statement

In addition to unwind_protect, Octave supports another limited form of exception handling.

The general form of a try block looks like this:

     try
       body
     catch
       cleanup
     end_try_catch

Where body and cleanup are both optional and may contain any Octave expressions or commands. The statements in cleanup are only executed if an error occurs in body.

No warnings or error messages are printed while body is executing. If an error does occur during the execution of body, cleanup can use the function lasterr to access the text of the message that would have been printed. This is the same as eval (try, catch) but it is more efficient since the commands do not need to be parsed each time the try and catch statements are evaluated. See Error Handling, for more information about the lasterr function.

Octave's try block is a very limited variation on the Lisp condition-case form (limited because it cannot handle different classes of errors separately). Perhaps at some point Octave can have some sort of classification of errors and try-catch can be improved to be as powerful as condition-case in Lisp.