Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > b38d2da330d1936e5ab1307c039c4941 > files > 433

octave-doc-3.6.4-3.mga4.noarch.rpm

<html lang="en">
<head>
<title>Recovering From Errors - GNU Octave</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="GNU Octave">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Handling-Errors.html#Handling-Errors" title="Handling Errors">
<link rel="prev" href="Catching-Errors.html#Catching-Errors" title="Catching Errors">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
  pre.display { font-family:inherit }
  pre.format  { font-family:inherit }
  pre.smalldisplay { font-family:inherit; font-size:smaller }
  pre.smallformat  { font-family:inherit; font-size:smaller }
  pre.smallexample { font-size:smaller }
  pre.smalllisp    { font-size:smaller }
  span.sc    { font-variant:small-caps }
  span.roman { font-family:serif; font-weight:normal; } 
  span.sansserif { font-family:sans-serif; font-weight:normal; } 
--></style>
</head>
<body>
<div class="node">
<a name="Recovering-From-Errors"></a>
<p>
Previous:&nbsp;<a rel="previous" accesskey="p" href="Catching-Errors.html#Catching-Errors">Catching Errors</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Handling-Errors.html#Handling-Errors">Handling Errors</a>
<hr>
</div>

<h4 class="subsection">12.1.3 Recovering From Errors</h4>

<p>Octave provides several ways of recovering from errors.  There are
<code>try</code>/<code>catch</code> blocks,
<code>unwind_protect</code>/<code>unwind_protect_cleanup</code> blocks,
and finally the <code>onCleanup</code> command.

   <p>The <code>onCleanup</code> command associates an ordinary Octave variable (the
trigger) with an arbitrary function (the action).  Whenever the Octave variable
ceases to exist&mdash;whether due to a function return, an error, or simply because
the variable has been removed with <code>clear</code>&mdash;then the assigned function
is executed.

   <p>The function can do anything necessary for cleanup such as closing open file
handles, printing an error message, or restoring global variables to their
initial values.  The last example is a very convenient idiom for Octave code. 
For example:

<pre class="example">     function rand42
       old_state = rand ('state');
       restore_state = onCleanup (@() rand ('state', old_state);
       rand ('state', 42);
       ...
     endfunction  # rand generator state restored by onCleanup
</pre>
   <!-- onCleanup src/ov-oncleanup.cc -->
   <p><a name="doc_002donCleanup"></a>

<div class="defun">
&mdash; Loadable Function: <var>c</var> = <b>onCleanup</b> (<var>action</var>)<var><a name="index-onCleanup-821"></a></var><br>
<blockquote><p>Create a special object that executes a given function upon destruction. 
If the object is copied to multiple variables (or cell or struct array
elements) or returned from a function, <var>action</var> will be executed after
clearing the last copy of the object.  Note that if multiple local onCleanup
variables are created, the order in which they are called is unspecified. 
For similar functionality See <a href="The-unwind_005fprotect-Statement.html#The-unwind_005fprotect-Statement">The unwind_protect Statement</a>. 
</p></blockquote></div>

   </body></html>