File: Catching-Errors.html

package info (click to toggle)
octave 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 124,192 kB
  • sloc: cpp: 322,665; ansic: 68,088; fortran: 20,980; objc: 8,121; sh: 7,719; yacc: 4,266; lex: 4,123; perl: 1,530; java: 1,366; awk: 1,257; makefile: 424; xml: 147
file content (302 lines) | stat: -rw-r--r-- 11,420 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Catching Errors (GNU Octave (version 6.2.0))</title>

<meta name="description" content="Catching Errors (GNU Octave (version 6.2.0))">
<meta name="keywords" content="Catching Errors (GNU Octave (version 6.2.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<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="Recovering-From-Errors.html" rel="next" title="Recovering From Errors">
<link href="Raising-Errors.html" rel="prev" title="Raising Errors">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">


</head>

<body lang="en">
<span id="Catching-Errors"></span><div class="header">
<p>
Next: <a href="Recovering-From-Errors.html" accesskey="n" rel="next">Recovering From Errors</a>, Previous: <a href="Raising-Errors.html" accesskey="p" rel="prev">Raising 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>
<span id="Catching-Errors-1"></span><h4 class="subsection">12.1.2 Catching Errors</h4>

<p>When an error occurs, it can be detected and handled using the
<code>try</code> statement as described in <a href="The-try-Statement.html">The try Statement</a>.
As an example, the following piece of code counts the number of errors
that occurs during a <code>for</code> loop.
</p>
<div class="example">
<pre class="example">number_of_errors = 0;
for n = 1:100
  try
    &hellip;
  catch
    number_of_errors++;
  end_try_catch
endfor
</pre></div>

<p>The above example treats all errors the same.  In many situations it
can however be necessary to discriminate between errors, and take
different actions depending on the error.  The <code>lasterror</code>
function returns a structure containing information about the last
error that occurred.  As an example, the code above could be changed
to count the number of errors related to the &lsquo;<samp>*</samp>&rsquo; operator.
</p>
<div class="example">
<pre class="example">number_of_errors = 0;
for n = 1:100
  try
    &hellip;
  catch
    msg = lasterror.message;
    if (strfind (msg, &quot;operator *&quot;))
      number_of_errors++;
    endif
  end_try_catch
endfor
</pre></div>

<p>Alternatively, the output of the <code>lasterror</code> function can be found
in a variable indicated immediately after the <code>catch</code> keyword, as
in the example below showing how to redirect an error as a warning:
</p>
<div class="example">
<pre class="example">try
  &hellip;
catch err
  warning(err.identifier, err.message);
  &hellip;
end_try_catch
</pre></div>

<span id="XREFlasterror"></span><dl>
<dt id="index-lasterror">: <em><var>lasterr</var> =</em> <strong>lasterror</strong> <em>()</em></dt>
<dt id="index-lasterror-1">: <em></em> <strong>lasterror</strong> <em>(<var>err</var>)</em></dt>
<dt id="index-lasterror-2">: <em></em> <strong>lasterror</strong> <em>(&quot;reset&quot;)</em></dt>
<dd><p>Query or set the last error message structure.
</p>
<p>When called without arguments, return a structure containing the last error
message and other information related to this error.  The elements of the
structure are:
</p>
<dl compact="compact">
<dt><code>message</code></dt>
<dd><p>The text of the last error message
</p>
</dd>
<dt><code>identifier</code></dt>
<dd><p>The message identifier of this error message
</p>
</dd>
<dt><code>stack</code></dt>
<dd><p>A structure containing information on where the message occurred.  This may
be an empty structure if the information cannot be obtained.  The fields of
the structure are:
</p>
<dl compact="compact">
<dt><code>file</code></dt>
<dd><p>The name of the file where the error occurred
</p>
</dd>
<dt><code>name</code></dt>
<dd><p>The name of function in which the error occurred
</p>
</dd>
<dt><code>line</code></dt>
<dd><p>The line number at which the error occurred
</p>
</dd>
<dt><code>column</code></dt>
<dd><p>An optional field with the column number at which the error occurred
</p></dd>
</dl>
</dd>
</dl>

<p>The last error structure may be set by passing a scalar structure,
<var>err</var>, as input.  Any fields of <var>err</var> that match those above are
set while any unspecified fields are initialized with default values.
</p>
<p>If <code>lasterror</code> is called with the argument <code>&quot;reset&quot;</code>, all
fields are set to their default values.
</p>
<p><strong>See also:</strong> <a href="#XREFlasterr">lasterr</a>, <a href="Raising-Errors.html#XREFerror">error</a>, <a href="Issuing-Warnings.html#XREFlastwarn">lastwarn</a>.
</p></dd></dl>


<span id="XREFlasterr"></span><dl>
<dt id="index-lasterr">: <em>[<var>msg</var>, <var>msgid</var>] =</em> <strong>lasterr</strong> <em>()</em></dt>
<dt id="index-lasterr-1">: <em></em> <strong>lasterr</strong> <em>(<var>msg</var>)</em></dt>
<dt id="index-lasterr-2">: <em></em> <strong>lasterr</strong> <em>(<var>msg</var>, <var>msgid</var>)</em></dt>
<dd><p>Query or set the last error message.
</p>
<p>When called without input arguments, return the last error message and
message identifier.
</p>
<p>With one argument, set the last error message to <var>msg</var>.
</p>
<p>With two arguments, also set the last message identifier.
</p>
<p><strong>See also:</strong> <a href="#XREFlasterror">lasterror</a>, <a href="Raising-Errors.html#XREFerror">error</a>, <a href="Issuing-Warnings.html#XREFlastwarn">lastwarn</a>.
</p></dd></dl>


<p>The next example counts indexing errors.  The errors are caught using the
field identifier of the structure returned by the function <code>lasterror</code>.
</p>
<div class="example">
<pre class="example">number_of_errors = 0;
for n = 1:100
  try
    &hellip;
  catch
    id = lasterror.identifier;
    if (strcmp (id, &quot;Octave:invalid-indexing&quot;))
      number_of_errors++;
    endif
  end_try_catch
endfor
</pre></div>

<p>The functions distributed with Octave can issue one of the following
errors.
</p>
<span id="XREFerror_005fids"></span><span id="index-error-ids"></span>

<dl compact="compact">
<dt><code>Octave:bad-alloc</code></dt>
<dd><p>Indicates that memory couldn&rsquo;t be allocated.
</p>
</dd>
<dt><code>Octave:invalid-context</code></dt>
<dd><p>Indicates the error was generated by an operation that cannot be executed in
the scope from which it was called.  For example, the function
<code>print_usage ()</code> when called from the Octave prompt raises this error.
</p>
</dd>
<dt><code>Octave:invalid-fun-call</code></dt>
<dd><p>Indicates that a function was called in an incorrect way, e.g., wrong number
of input arguments.
</p>
</dd>
<dt><code>Octave:invalid-indexing</code></dt>
<dd><p>Indicates that a data-type was indexed incorrectly, e.g., real-value index
for arrays, nonexistent field of a structure.
</p>
</dd>
<dt><code>Octave:invalid-input-arg</code></dt>
<dd><p>Indicates that a function was called with invalid input arguments.
</p>
</dd>
<dt><code>Octave:undefined-function</code></dt>
<dd><p>Indicates a call to a function that is not defined.  The function may exist
but Octave is unable to find it in the search path.
</p>
</dd>
</dl>



<p>When an error has been handled it is possible to raise it again.  This
can be useful when an error needs to be detected, but the program should
still abort.  This is possible using the <code>rethrow</code> function.  The
previous example can now be changed to count the number of errors
related to the &lsquo;<samp>*</samp>&rsquo; operator, but still abort if another kind of
error occurs.
</p>
<div class="example">
<pre class="example">number_of_errors = 0;
for n = 1:100
  try
    &hellip;
  catch
    msg = lasterror.message;
    if (strfind (msg, &quot;operator *&quot;))
      number_of_errors++;
    else
      rethrow (lasterror);
    endif
  end_try_catch
endfor
</pre></div>

<span id="XREFrethrow"></span><dl>
<dt id="index-rethrow">: <em></em> <strong>rethrow</strong> <em>(<var>err</var>)</em></dt>
<dd><p>Reissue a previous error as defined by <var>err</var>.
</p>
<p><var>err</var> is a structure that must contain at least the <code>&quot;message&quot;</code>
and <code>&quot;identifier&quot;</code> fields.  <var>err</var> can also contain a field
<code>&quot;stack&quot;</code> that gives information on the assumed location of the
error.  Typically <var>err</var> is returned from <code>lasterror</code>.
</p>
<p><strong>See also:</strong> <a href="#XREFlasterror">lasterror</a>, <a href="#XREFlasterr">lasterr</a>, <a href="Raising-Errors.html#XREFerror">error</a>.
</p></dd></dl>



<span id="XREFerrno"></span><dl>
<dt id="index-errno">: <em><var>err</var> =</em> <strong>errno</strong> <em>()</em></dt>
<dt id="index-errno-1">: <em><var>err</var> =</em> <strong>errno</strong> <em>(<var>val</var>)</em></dt>
<dt id="index-errno-2">: <em><var>err</var> =</em> <strong>errno</strong> <em>(<var>name</var>)</em></dt>
<dd><p>Query or set the system-dependent variable errno.
</p>
<p>When called with no inputs, return the current value of errno.
</p>
<p>When called with a numeric input <var>val</var>, set the current value of errno
to the specified value.  The previous value of errno is returned as <var>err</var>.
</p>
<p>When called with a character string <var>name</var>, return the numeric value of
errno which corresponds to the specified error code.  If <var>name</var> is not
a recognized error code then -1 is returned.
</p>

<p><strong>See also:</strong> <a href="#XREFerrno_005flist">errno_list</a>.
</p></dd></dl>


<span id="XREFerrno_005flist"></span><dl>
<dt id="index-errno_005flist">: <em></em> <strong>errno_list</strong> <em>()</em></dt>
<dd><p>Return a structure containing the system-dependent errno values.
</p>
<p><strong>See also:</strong> <a href="#XREFerrno">errno</a>.
</p></dd></dl>


<hr>
<div class="header">
<p>
Next: <a href="Recovering-From-Errors.html" accesskey="n" rel="next">Recovering From Errors</a>, Previous: <a href="Raising-Errors.html" accesskey="p" rel="prev">Raising 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>



</body>
</html>