| GNU Octave Manual by John W. Eaton Paperback (6"x9"), 324 pages, 4 figures ISBN 0954161726 RRP £19.99 ($29.99) |
10.7 The unwind_protect Statement
Octave supports a limited form of exception handling modelled after the unwind-protect form of Lisp.
The general form of an unwind_protect block looks like this:
unwind_protect body unwind_protect_cleanup cleanup end_unwind_protect
Where body and cleanup are both optional and may contain any Octave expressions or commands. The statements in cleanup are guaranteed to be executed regardless of how control exits body.
This is useful to protect temporary changes to global variables from
possible errors. For example, the following code will always restore
the original value of the built-in variable do_fortran_indexing
even if an error occurs while performing the indexing operation.
save_do_fortran_indexing = do_fortran_indexing; unwind_protect do_fortran_indexing = 1; elt = a (idx) unwind_protect_cleanup do_fortran_indexing = save_do_fortran_indexing; end_unwind_protect
Without unwind_protect, the value of do_fortran_indexing
would not be restored if an error occurs while performing the indexing
operation because evaluation would stop at the point of the error and
the statement to restore the value would not be executed.
| ISBN 0954161726 | GNU Octave Manual | See the print edition |