<html lang="en"> <head> <title>Catching 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="Raising-Errors.html#Raising-Errors" title="Raising Errors"> <link rel="next" href="Recovering-From-Errors.html#Recovering-From-Errors" title="Recovering From 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="Catching-Errors"></a> <p> Next: <a rel="next" accesskey="n" href="Recovering-From-Errors.html#Recovering-From-Errors">Recovering From Errors</a>, Previous: <a rel="previous" accesskey="p" href="Raising-Errors.html#Raising-Errors">Raising Errors</a>, Up: <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-try-Statement.html#The-try-Statement">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. <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 ‘<samp><span class="samp">*</span></samp>’ 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> <!-- lasterror src/error.cc --> <p><a name="doc_002dlasterror"></a> <div class="defun"> — Built-in Function: <var>lasterr</var> = <b>lasterror</b> ()<var><a name="index-lasterror-810"></a></var><br> — Built-in Function: <b>lasterror</b> (<var>err</var>)<var><a name="index-lasterror-811"></a></var><br> — Built-in Function: <b>lasterror</b> (<var>'reset'</var>)<var><a name="index-lasterror-812"></a></var><br> <blockquote><p>Query or set the last error message structure. 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: <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 may be an empty structure if the information cannot be obtained. The fields of the 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 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>If <code>lasterror</code> is called with the argument 'reset', all fields are set to their default values. <!-- 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_002dlasterr.html#doc_002dlasterr">lasterr</a>. </p></blockquote></div> <!-- lasterr src/error.cc --> <p><a name="doc_002dlasterr"></a> <div class="defun"> — Built-in Function: [<var>msg</var>, <var>msgid</var>] = <b>lasterr</b> ()<var><a name="index-lasterr-813"></a></var><br> — Built-in Function: <b>lasterr</b> (<var>msg</var>)<var><a name="index-lasterr-814"></a></var><br> — Built-in Function: <b>lasterr</b> (<var>msg, msgid</var>)<var><a name="index-lasterr-815"></a></var><br> <blockquote><p>Query or set the last error message. When called without input arguments, return the last error message and message identifier. With one argument, set the last error message to <var>msg</var>. With two arguments, also set the last message identifier. <!-- 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>. </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 ‘<samp><span class="samp">*</span></samp>’ 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> <!-- rethrow src/error.cc --> <p><a name="doc_002drethrow"></a> <div class="defun"> — Built-in Function: <b>rethrow</b> (<var>err</var>)<var><a name="index-rethrow-816"></a></var><br> <blockquote><p>Reissue 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... --> <!-- errno src/utils.cc --> <p><a name="doc_002derrno"></a> <div class="defun"> — Built-in Function: <var>err</var> = <b>errno</b> ()<var><a name="index-errno-817"></a></var><br> — Built-in Function: <var>err</var> = <b>errno</b> (<var>val</var>)<var><a name="index-errno-818"></a></var><br> — Built-in Function: <var>err</var> = <b>errno</b> (<var>name</var>)<var><a name="index-errno-819"></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> <!-- errno_list src/utils.cc --> <p><a name="doc_002derrno_005flist"></a> <div class="defun"> — Built-in Function: <b>errno_list</b> ()<var><a name="index-errno_005flist-820"></a></var><br> <blockquote><p>Return a structure containing the system-dependent errno values. </p></blockquote></div> </body></html>