File: Catching-Errors.html

package info (click to toggle)
octave3.2 3.2.4-8
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 62,936 kB
  • ctags: 37,353
  • sloc: cpp: 219,497; fortran: 116,336; ansic: 10,264; sh: 5,508; makefile: 4,245; lex: 3,573; yacc: 3,062; objc: 2,042; lisp: 1,692; awk: 860; perl: 844
file content (174 lines) | stat: -rw-r--r-- 7,643 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
<html lang="en">
<head>
<title>Catching Errors - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.11">
<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="Raising-Errors.html#Raising-Errors" title="Raising 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">
<p>
<a name="Catching-Errors"></a>
Previous:&nbsp;<a rel="previous" accesskey="p" href="Raising-Errors.html#Raising-Errors">Raising 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.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-_003ccode_003etry_003c_002fcode_003e-Statement.html#The-_003ccode_003etry_003c_002fcode_003e-Statement">The <code>try</code> Statement</a>. 
As an example, the following piece of code counts the number of errors
that occurs during a <code>for</code> loop.

<pre class="example">     number_of_errors = 0;
     for n = 1:100
       try
         ...
       catch
         number_of_errors++;
       end_try_catch
     endfor
</pre>
   <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><span class="samp">*</span></samp>&rsquo; operator.

<pre class="example">     number_of_errors = 0;
     for n = 1:100
       try
         ...
       catch
         msg = lasterror.message;
         if (strfind (msg, "operator *"))
           number_of_errors++;
         endif
       end_try_catch
     endfor
</pre>
   <!-- error.cc -->
   <p><a name="doc_002dlasterror"></a>

<div class="defun">
&mdash; Built-in Function: <var>err</var> = <b>lasterror</b> (<var>err</var>)<var><a name="index-lasterror-670"></a></var><br>
&mdash; Built-in Function:  <b>lasterror</b> (<var>'reset'</var>)<var><a name="index-lasterror-671"></a></var><br>
<blockquote><p>Returns or sets the last error message.  Called without any arguments
returns a structure containing the last error message, as well as other
information related to this error.  The elements of this structure are:

          <dl>
<dt>'message'<dd>The text of the last error message
<br><dt>'identifier'<dd>The message identifier of this error message
<br><dt>'stack'<dd>A structure containing information on where the message occurred.  This might
be an empty structure if this in the case where this information cannot
be obtained.  The fields of this structure are:

               <dl>
<dt>'file'<dd>The name of the file where the error occurred
<br><dt>'name'<dd>The name of function in which the error occurred
<br><dt>'line'<dd>The line number at which the error occurred
<br><dt>'column'<dd>An optional field with the column number at which the error occurred
</dl>
          </dl>

        <p>The <var>err</var> structure may also be passed to <code>lasterror</code> to set the
information about the last error.  The only constraint on <var>err</var> in that
case is that it is a scalar structure.  Any fields of <var>err</var> that match
the above are set to the value passed in <var>err</var>, while other fields are
set to their default values.

        <p>If <code>lasterror</code> is called with the argument 'reset', all values take
their default values. 
</p></blockquote></div>

<!-- error.cc -->
   <p><a name="doc_002dlasterr"></a>

<div class="defun">
&mdash; Built-in Function: [<var>msg</var>, <var>msgid</var>] = <b>lasterr</b> (<var>msg, msgid</var>)<var><a name="index-lasterr-672"></a></var><br>
<blockquote><p>Without any arguments, return the last error message.  With one
argument, set the last error message to <var>msg</var>.  With two arguments,
also set the last message identifier. 
</p></blockquote></div>

   <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><span class="samp">*</span></samp>&rsquo; operator, but still abort if another kind of
error occurs.

<pre class="example">     number_of_errors = 0;
     for n = 1:100
       try
         ...
       catch
         msg = lasterror.message;
         if (strfind (msg, "operator *"))
           number_of_errors++;
         else
           rethrow (lasterror);
         endif
       end_try_catch
     endfor
</pre>
   <!-- error.cc -->
   <p><a name="doc_002drethrow"></a>

<div class="defun">
&mdash; Built-in Function:  <b>rethrow</b> (<var>err</var>)<var><a name="index-rethrow-673"></a></var><br>
<blockquote><p>Reissues a previous error as defined by <var>err</var>.  <var>err</var> is a structure
that must contain at least the 'message' and 'identifier' fields.  <var>err</var>
can also contain a field 'stack' that gives information on the assumed
location of the error.  Typically <var>err</var> is returned from
<code>lasterror</code>. 
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->

     <p class="noindent"><strong>See also:</strong> <a href="doc_002dlasterror.html#doc_002dlasterror">lasterror</a>, <a href="doc_002dlasterr.html#doc_002dlasterr">lasterr</a>, <a href="doc_002derror.html#doc_002derror">error</a>. 
</p></blockquote></div>

<!-- FIXME: I have no idea what the rest of the functions are used for... -->
<!-- utils.cc -->
   <p><a name="doc_002derrno"></a>

<div class="defun">
&mdash; Built-in Function: <var>err</var> = <b>errno</b> ()<var><a name="index-errno-674"></a></var><br>
&mdash; Built-in Function: <var>err</var> = <b>errno</b> (<var>val</var>)<var><a name="index-errno-675"></a></var><br>
&mdash; Built-in Function: <var>err</var> = <b>errno</b> (<var>name</var>)<var><a name="index-errno-676"></a></var><br>
<blockquote><p>Return the current value of the system-dependent variable errno,
set its value to <var>val</var> and return the previous value, or return
the named error code given <var>name</var> as a character string, or -1
if <var>name</var> is not found. 
</p></blockquote></div>

<!-- utils.cc -->
   <p><a name="doc_002derrno_005flist"></a>

<div class="defun">
&mdash; Built-in Function:  <b>errno_list</b> ()<var><a name="index-errno_005flist-677"></a></var><br>
<blockquote><p>Return a structure containing the system-dependent errno values. 
</p></blockquote></div>

   </body></html>