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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Raising Errors (GNU Octave (version 6.2.0))</title>
<meta name="description" content="Raising Errors (GNU Octave (version 6.2.0))">
<meta name="keywords" content="Raising Errors (GNU Octave (version 6.2.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Handling-Errors.html" rel="up" title="Handling Errors">
<link href="Catching-Errors.html" rel="next" title="Catching Errors">
<link href="Handling-Errors.html" rel="prev" title="Handling Errors">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<span id="Raising-Errors"></span><div class="header">
<p>
Next: <a href="Catching-Errors.html" accesskey="n" rel="next">Catching Errors</a>, Up: <a href="Handling-Errors.html" accesskey="u" rel="up">Handling Errors</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<span id="Raising-Errors-1"></span><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.
</p>
<div class="example">
<pre class="example">function f (arg1)
if (nargin == 0)
error ("not enough input arguments");
endif
endfunction
</pre></div>
<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.
</p>
<p>It is also possible to assign an identification string to an error.
If an error has such an ID the user can catch this error
as will be described in the next section. To assign an ID to an error,
simply call <code>error</code> with two string arguments, where the first
is the identification string, and the second is the actual error. Note
that error IDs are in the format <code>"NAMESPACE:ERROR-NAME"</code>. The namespace
<code>"Octave"</code> is used for Octave’s own errors. Any other string is
available as a namespace for user’s own errors.
</p>
<span id="XREFerror"></span><dl>
<dt id="index-error">: <em></em> <strong>error</strong> <em>(<var>template</var>, …)</em></dt>
<dt id="index-error-1">: <em></em> <strong>error</strong> <em>(<var>id</var>, <var>template</var>, …)</em></dt>
<dd><p>Display an error message and stop m-file execution.
</p>
<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</a>) and print the resulting message
on the <code>stderr</code> stream. The message is prefixed by the character
string ‘<samp>error: </samp>’.
</p>
<p>Calling <code>error</code> also sets Octave’s internal error state such that
control will return to the top level without evaluating any further
commands. This is useful for aborting from functions or scripts.
</p>
<p>If the error message does not end with a newline character, Octave will
print a traceback of all the function calls leading to the error. For
example, given the following function definitions:
</p>
<div class="example">
<pre class="example">function f () g (); end
function g () h (); end
function h () nargin == 1 || error ("nargin != 1"); end
</pre></div>
<p>calling the function <code>f</code> will result in a list of messages that
can help you to quickly find the exact location of the error:
</p>
<div class="example">
<pre class="example">f ()
error: nargin != 1
error: called from:
error: h at line 1, column 27
error: g at line 1, column 15
error: f at line 1, column 15
</pre></div>
<p>If the error message ends in a newline 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 newline causes Octave to only print
a single message:
</p>
<div class="example">
<pre class="example">function h () nargin == 1 || error ("nargin != 1\n"); end
f ()
error: nargin != 1
</pre></div>
<p>A null string ("") input to <code>error</code> will be ignored and the code
will continue running as if the statement were a NOP. This is for
compatibility with <small>MATLAB</small>. It also makes it possible to write code
such as
</p>
<div class="example">
<pre class="example">err_msg = "";
if (CONDITION 1)
err_msg = "CONDITION 1 found";
elseif (CONDITION2)
err_msg = "CONDITION 2 found";
…
endif
error (err_msg);
</pre></div>
<p>which will only stop execution if an error has been found.
</p>
<p>Implementation Note: For compatibility with <small>MATLAB</small>, escape
sequences in <var>template</var> (e.g., <code>"\n"</code> =>
newline) are processed regardless of whether <var>template</var> has been defined
with single quotes, as long as there are two or more input arguments. To
disable escape sequence expansion use a second backslash before the sequence
(e.g., <code>"\\n"</code>) or use the
<code>regexptranslate</code> function.
</p>
<p><strong>See also:</strong> <a href="Issuing-Warnings.html#XREFwarning">warning</a>, <a href="Catching-Errors.html#XREFlasterror">lasterror</a>.
</p></dd></dl>
<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>
<p>Consider the following function.
</p>
<div class="example">
<pre class="example">## -*- texinfo -*-
## @deftypefn {} f (@var{arg1})
## Function help text goes here…
## @end deftypefn
function f (arg1)
if (nargin == 0)
print_usage ();
endif
endfunction
</pre></div>
<p>When it is called with no input arguments it produces the following
error.
</p>
<div class="example">
<pre class="example">f ()
-| error: Invalid call to f. Correct usage is:
-|
-| -- f (ARG1)
-|
-|
-| Additional help for built-in functions and operators is
-| available in the online 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 https://www.octave.org and via the help@octave.org
-| mailing list.
</pre></div>
<span id="XREFprint_005fusage"></span><dl>
<dt id="index-print_005fusage">: <em></em> <strong>print_usage</strong> <em>()</em></dt>
<dt id="index-print_005fusage-1">: <em></em> <strong>print_usage</strong> <em>(<var>name</var>)</em></dt>
<dd><p>Print the usage message for the function <var>name</var>.
</p>
<p>When called with no input arguments the <code>print_usage</code> function displays
the usage message of the currently executing function.
</p>
<p><strong>See also:</strong> <a href="Getting-Help.html#XREFhelp">help</a>.
</p></dd></dl>
<span id="XREFbeep"></span><dl>
<dt id="index-beep">: <em></em> <strong>beep</strong> <em>()</em></dt>
<dd><p>Produce a beep from the speaker (or visual bell).
</p>
<p>This function sends the alarm character <code>"\a"</code> to
the terminal. Depending on the user’s configuration this may produce an
audible beep, a visual bell, or nothing at all.
</p>
<p><strong>See also:</strong> <a href="Simple-Output.html#XREFputs">puts</a>, <a href="Simple-Output.html#XREFfputs">fputs</a>, <a href="Formatted-Output.html#XREFprintf">printf</a>, <a href="Formatted-Output.html#XREFfprintf">fprintf</a>.
</p></dd></dl>
<span id="XREFbeep_005fon_005ferror"></span><dl>
<dt id="index-beep_005fon_005ferror">: <em><var>val</var> =</em> <strong>beep_on_error</strong> <em>()</em></dt>
<dt id="index-beep_005fon_005ferror-1">: <em><var>old_val</var> =</em> <strong>beep_on_error</strong> <em>(<var>new_val</var>)</em></dt>
<dt id="index-beep_005fon_005ferror-2">: <em></em> <strong>beep_on_error</strong> <em>(<var>new_val</var>, "local")</em></dt>
<dd><p>Query or set the internal variable that controls whether Octave will try
to ring the terminal bell before printing an error message.
</p>
<p>When called from inside a function with the <code>"local"</code> option, the
variable is changed locally for the function and any subroutines it calls.
The original variable value is restored when exiting the function.
</p></dd></dl>
<hr>
<div class="header">
<p>
Next: <a href="Catching-Errors.html" accesskey="n" rel="next">Catching Errors</a>, Up: <a href="Handling-Errors.html" accesskey="u" rel="up">Handling Errors</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|