File: Recovering-From-Errors.html

package info (click to toggle)
octave 10.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 145,388 kB
  • sloc: cpp: 335,976; ansic: 82,241; fortran: 20,963; objc: 9,402; sh: 8,756; yacc: 4,392; lex: 4,333; perl: 1,544; java: 1,366; awk: 1,259; makefile: 660; xml: 192
file content (108 lines) | stat: -rw-r--r-- 5,038 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Recovering From Errors (GNU Octave (version 10.3.0))</title>

<meta name="description" content="Recovering From Errors (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Recovering From Errors (GNU Octave (version 10.3.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Handling-Errors.html" rel="up" title="Handling Errors">
<link href="Catching-Errors.html" rel="prev" title="Catching Errors">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">


</head>

<body lang="en">
<div class="subsection-level-extent" id="Recovering-From-Errors">
<div class="nav-panel">
<p>
Previous: <a href="Catching-Errors.html" accesskey="p" rel="prev">Catching Errors</a>, Up: <a href="Handling-Errors.html" accesskey="u" rel="up">Handling Errors</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="Recovering-From-Errors-1"><span>12.1.3 Recovering From Errors<a class="copiable-link" href="#Recovering-From-Errors-1"> &para;</a></span></h4>

<p>Octave provides several ways of recovering from errors.  There are
<code class="code">try</code>/<code class="code">catch</code> blocks,
<code class="code">unwind_protect</code>/<code class="code">unwind_protect_cleanup</code> blocks,
and finally the <code class="code">onCleanup</code> command.
</p>
<p>The <code class="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 class="code">clear</code>&mdash;then the assigned function
is executed.
</p>
<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:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">function rand42
  old_state = rand (&quot;state&quot;);
  restore_state = onCleanup (@() rand (&quot;state&quot;, old_state));
  rand (&quot;state&quot;, 42);
  ...
endfunction  # rand generator state restored by onCleanup
</pre></div></div>

<a class="anchor" id="XREFonCleanup"></a><span style="display:block; margin-top:-4.5ex;">&nbsp;</span>


<dl class="first-deftypefn">
<dt class="deftypefn" id="index-onCleanup"><span><code class="def-type"><var class="var">obj</var> =</code> <strong class="def-name">onCleanup</strong> <code class="def-code-arguments">(<var class="var">function</var>)</code><a class="copiable-link" href="#index-onCleanup"> &para;</a></span></dt>
<dd><p>Create a special object that executes a given <var class="var">function</var> upon destruction.
</p>
<p>If the object is copied to multiple variables (or cell or struct array
elements) or returned from a function, then <var class="var">function</var> will be executed
only after the last copy of the object is cleared.
</p>
<p>The input <var class="var">function</var> is a handle to a function.  The handle may point to an
anonymous function in order to directly place commands in the <code class="code">onCleanup</code>
call.
</p>
<p>Programming Note: If multiple local <code class="code">onCleanup</code> variables are created, the
order in which they are called is unspecified.  For similar functionality
See <a class="xref" href="The-unwind_005fprotect-Statement.html">The unwind_protect Statement</a>.
</p>
<p>Example
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">octave:1&gt; trigger = onCleanup (@() disp ('onCleanup was executed'));
octave:2&gt; clear trigger
onCleanup was executed
octave:3
</pre></div></div>

</dd></dl>


</div>
<hr>
<div class="nav-panel">
<p>
Previous: <a href="Catching-Errors.html">Catching Errors</a>, Up: <a href="Handling-Errors.html">Handling Errors</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>