Sophie

Sophie

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

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

<html lang="en">
<head>
<title>The unwind_protect Statement - 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="Statements.html#Statements" title="Statements">
<link rel="prev" href="The-continue-Statement.html#The-continue-Statement" title="The continue Statement">
<link rel="next" href="The-try-Statement.html#The-try-Statement" title="The try Statement">
<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="The-unwind_protect-Statement"></a>
<a name="The-unwind_005fprotect-Statement"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="The-try-Statement.html#The-try-Statement">The try Statement</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="The-continue-Statement.html#The-continue-Statement">The continue Statement</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Statements.html#Statements">Statements</a>
<hr>
</div>

<h3 class="section">10.8 The unwind_protect Statement</h3>

<p><a name="index-g_t_0040code_007bunwind_005fprotect_007d-statement-709"></a><a name="index-g_t_0040code_007bunwind_005fprotect_005fcleanup_007d-710"></a><a name="index-g_t_0040code_007bend_005funwind_005fprotect_007d-711"></a>
Octave supports a limited form of exception handling modelled after the
unwind-protect form of Lisp.

   <p>The general form of an <code>unwind_protect</code> block looks like this:

<pre class="example">     unwind_protect
       <var>body</var>
     unwind_protect_cleanup
       <var>cleanup</var>
     end_unwind_protect
</pre>
   <p class="noindent">where <var>body</var> and <var>cleanup</var> are both optional and may contain any
Octave expressions or commands.  The statements in <var>cleanup</var> are
guaranteed to be executed regardless of how control exits <var>body</var>.

   <p>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 global variable <code>frobnosticate</code>
even if an error occurs in the first part of the <code>unwind_protect</code>
block.

<pre class="example">     save_frobnosticate = frobnosticate;
     unwind_protect
       frobnosticate = true;
       ...
     unwind_protect_cleanup
       frobnosticate = save_frobnosticate;
     end_unwind_protect
</pre>
   <p class="noindent">Without <code>unwind_protect</code>, the value of <var>frobnosticate</var>
would not be restored if an error occurs while evaluating the first part
of the <code>unwind_protect</code> block because evaluation would stop at the
point of the error and the statement to restore the value would not be
executed.

   </body></html>