<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.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 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> 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-_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 ‘<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> <!-- error.cc --> <p><a name="doc_002dlasterror"></a> <div class="defun"> — Built-in Function: <var>err</var> = <b>lasterror</b> (<var>err</var>)<var><a name="index-lasterror-670"></a></var><br> — 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"> — 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 ‘<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> <!-- 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-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"> — Built-in Function: <var>err</var> = <b>errno</b> ()<var><a name="index-errno-674"></a></var><br> — Built-in Function: <var>err</var> = <b>errno</b> (<var>val</var>)<var><a name="index-errno-675"></a></var><br> — 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"> — 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>