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
|
<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.11">
<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">
<p>
<a name="Raising-Errors"></a>
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>
|