<html lang="en"> <head> <title>Raising 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="next" href="Catching-Errors.html#Catching-Errors" title="Catching 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="Raising-Errors"></a> <p> Next: <a rel="next" accesskey="n" href="Catching-Errors.html#Catching-Errors">Catching Errors</a>, Up: <a rel="up" accesskey="u" href="Handling-Errors.html#Handling-Errors">Handling Errors</a> <hr> </div> <h4 class="subsection">12.1.1 Raising Errors</h4> <p>The most common use of errors is for checking input arguments to functions. The following example calls the <code>error</code> function if the function <code>f</code> is called without any input arguments. <pre class="example"> function f (arg1) if (nargin == 0) error("not enough input arguments"); endif endfunction </pre> <p>When the <code>error</code> function is called, it prints the given message and returns to the Octave prompt. This means that no code following a call to <code>error</code> will be executed. <!-- error.cc --> <p><a name="doc_002derror"></a> <div class="defun"> — Built-in Function: <b>error</b> (<var>template, <small class="dots">...</small></var>)<var><a name="index-error-662"></a></var><br> — Built-in Function: <b>error</b> (<var>id, template, <small class="dots">...</small></var>)<var><a name="index-error-663"></a></var><br> <blockquote><p>Format the optional arguments under the control of the template string <var>template</var> using the same rules as the <code>printf</code> family of functions (see <a href="Formatted-Output.html#Formatted-Output">Formatted Output</a>) and print the resulting message on the <code>stderr</code> stream. The message is prefixed by the character string ‘<samp><span class="samp">error: </span></samp>’. <p>Calling <code>error</code> also sets Octave's internal error state such that control will return to the top level without evaluating any more commands. This is useful for aborting from functions or scripts. <p>If the error message does not end with a new line character, Octave will print a traceback of all the function calls leading to the error. For example, given the following function definitions: <pre class="example"> function f () g (); end function g () h (); end function h () nargin == 1 || error ("nargin != 1"); end </pre> <p class="noindent">calling the function <code>f</code> will result in a list of messages that can help you to quickly locate the exact location of the error: <pre class="example"> f () error: nargin != 1 error: called from: error: error at line -1, column -1 error: h at line 1, column 27 error: g at line 1, column 15 error: f at line 1, column 15 </pre> <p>If the error message ends in a new line character, Octave will print the message but will not display any traceback messages as it returns control to the top level. For example, modifying the error message in the previous example to end in a new line causes Octave to only print a single message: <pre class="example"> function h () nargin == 1 || error ("nargin != 1\n"); end f () error: nargin != 1 </pre> </blockquote></div> <p>Since it is common to use errors when there is something wrong with the input to a function, Octave supports functions to simplify such code. When the <code>print_usage</code> function is called, it reads the help text of the function calling <code>print_usage</code>, and presents a useful error. If the help text is written in Texinfo it is possible to present an error message that only contains the function prototypes as described by the <code>@deftypefn</code> parts of the help text. When the help text isn't written in Texinfo, the error message contains the entire help message. <p>Consider the following function. <pre class="example"> ## -*- texinfo -*- ## @deftypefn {Function File} f (@var{arg1}) ## Function help text goes here... ## @end deftypefn function f (arg1) if (nargin == 0) print_usage (); endif endfunction </pre> <p class="noindent">When it is called with no input arguments it produces the following error. <pre class="example"> f () -| error: Invalid call to f. Correct usage is: -| -| -- Function File: f (ARG1) -| -| -| Additional help for built-in functions and operators is -| available in the on-line version of the manual. Use the command -| `doc <topic>' to search the manual index. -| -| Help and information about Octave is also available on the WWW -| at http://www.octave.org and via the help@octave.org -| mailing list. </pre> <!-- ./help/print_usage.m --> <p><a name="doc_002dprint_005fusage"></a> <div class="defun"> — Function File: <b>print_usage</b> ()<var><a name="index-print_005fusage-664"></a></var><br> — Function File: <b>print_usage</b> (<var>name</var>)<var><a name="index-print_005fusage-665"></a></var><br> <blockquote><p>Print the usage message for a function. When called with no input arguments the <code>print_usage</code> function displays the usage message of the currently executing function. <!-- 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_002dhelp.html#doc_002dhelp">help</a>. </p></blockquote></div> <!-- error.cc --> <p><a name="doc_002dusage"></a> <div class="defun"> — Built-in Function: <b>usage</b> (<var>msg</var>)<var><a name="index-usage-666"></a></var><br> <blockquote><p>Print the message <var>msg</var>, prefixed by the string ‘<samp><span class="samp">usage: </span></samp>’, and set Octave's internal error state such that control will return to the top level without evaluating any more commands. This is useful for aborting from functions. <p>After <code>usage</code> is evaluated, Octave will print a traceback of all the function calls leading to the usage message. <p>You should use this function for reporting problems errors that result from an improper call to a function, such as calling a function with an incorrect number of arguments, or with arguments of the wrong type. For example, most functions distributed with Octave begin with code like this <pre class="example"> if (nargin != 2) usage ("foo (a, b)"); endif </pre> <p class="noindent">to check for the proper number of arguments. </p></blockquote></div> <!-- ./io/beep.m --> <p><a name="doc_002dbeep"></a> <div class="defun"> — Function File: <b>beep</b> ()<var><a name="index-beep-667"></a></var><br> <blockquote><p>Produce a beep from the speaker (or visual bell). <!-- 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_002dputs.html#doc_002dputs">puts</a>, <a href="doc_002dfputs.html#doc_002dfputs">fputs</a>, <a href="doc_002dprintf.html#doc_002dprintf">printf</a>, <a href="doc_002dfprintf.html#doc_002dfprintf">fprintf</a>. </p></blockquote></div> <!-- error.cc --> <p><a name="doc_002dbeep_005fon_005ferror"></a> <div class="defun"> — Built-in Function: <var>val</var> = <b>beep_on_error</b> ()<var><a name="index-beep_005fon_005ferror-668"></a></var><br> — Built-in Function: <var>old_val</var> = <b>beep_on_error</b> (<var>new_val</var>)<var><a name="index-beep_005fon_005ferror-669"></a></var><br> <blockquote><p>Query or set the internal variable that controls whether Octave will try to ring the terminal bell before printing an error message. </p></blockquote></div> </body></html>