File: Raising-Errors.html

package info (click to toggle)
octave 3.8.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 84,396 kB
  • ctags: 45,547
  • sloc: cpp: 293,356; ansic: 42,041; fortran: 23,669; sh: 13,629; objc: 7,890; yacc: 7,093; lex: 3,442; java: 2,125; makefile: 1,589; perl: 1,009; awk: 974; xml: 34
file content (265 lines) | stat: -rw-r--r-- 11,408 bytes parent folder | download | duplicates (3)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Octave: Raising Errors</title>

<meta name="description" content="GNU Octave: Raising Errors">
<meta name="keywords" content="GNU Octave: Raising Errors">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Handling-Errors.html#Handling-Errors" rel="up" title="Handling Errors">
<link href="Catching-Errors.html#Catching-Errors" rel="next" title="Catching Errors">
<link href="Handling-Errors.html#Handling-Errors" rel="prev" title="Handling Errors">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {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}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>


</head>

<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Raising-Errors"></a>
<div class="header">
<p>
Next: <a href="Catching-Errors.html#Catching-Errors" accesskey="n" rel="next">Catching Errors</a>, Up: <a href="Handling-Errors.html#Handling-Errors" accesskey="u" rel="up">Handling Errors</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Raising-Errors-1"></a>
<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 (&quot;not enough input arguments&quot;);
  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>
<a name="XREFerror"></a><dl>
<dt><a name="index-error"></a>Built-in Function: <em></em> <strong>error</strong> <em>(<var>template</var>, &hellip;)</em></dt>
<dt><a name="index-error-1"></a>Built-in Function: <em></em> <strong>error</strong> <em>(<var>id</var>, <var>template</var>, &hellip;)</em></dt>
<dd><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 &lsquo;<samp>error: </samp>&rsquo;.
</p>
<p>Calling <code>error</code> also sets Octave&rsquo;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>
<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:
</p>
<div class="example">
<pre class="example">function f () g (); end
function g () h (); end
function h () nargin == 1 || error (&quot;nargin != 1&quot;); end
</pre></div>

<p>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:
</p>
<div class="example">
<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></div>

<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:
</p>
<div class="example">
<pre class="example">function h () nargin == 1 || error (&quot;nargin != 1\n&quot;); end
f ()
error: nargin != 1
</pre></div>

<p>A null string (&quot;&quot;) 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 = &quot;&quot;;
if (CONDITION 1)
  err_msg = &quot;CONDITION 1 found&quot;;
elseif (CONDITION2)
  err_msg = &quot;CONDITION 2 found&quot;;
&hellip;
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 (e.g., <code>&quot;\n&quot;</code> =&gt; newline) are processed in <var>template</var>
regardless of whether <var>template</var> has been defined within single quotes
as long as there are two or more input arguments.
Use a second backslash to stop interpolation of the escape sequence (e.g.,
&quot;\\n&quot;) 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&rsquo;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 {Function File} f (@var{arg1})
## Function help text goes here&hellip;
## @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:
-|  
-|   -- Function File: f (ARG1)
-|  
-|  
-|  Additional help for built-in functions and operators is
-|  available in the online version of the manual.  Use the command
-|  `doc &lt;topic&gt;' 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></div>

<a name="XREFprint_005fusage"></a><dl>
<dt><a name="index-print_005fusage"></a>Function File: <em></em> <strong>print_usage</strong> <em>()</em></dt>
<dt><a name="index-print_005fusage-1"></a>Function File: <em></em> <strong>print_usage</strong> <em>(<var>name</var>)</em></dt>
<dd><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.
</p>
<p><strong>See also:</strong> <a href="Getting-Help.html#XREFhelp">help</a>.
</p></dd></dl>


<a name="XREFusage"></a><dl>
<dt><a name="index-usage"></a>Built-in Function: <em></em> <strong>usage</strong> <em>(<var>msg</var>)</em></dt>
<dd><p>Print the message <var>msg</var>, prefixed by the string &lsquo;<samp>usage: </samp>&rsquo;, and
set Octave&rsquo;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>
<p>After <code>usage</code> is evaluated, Octave will print a traceback of all
the function calls leading to the usage message.
</p>
<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
</p>
<div class="example">
<pre class="example">if (nargin != 2)
  usage (&quot;foo (a, b)&quot;);
endif
</pre></div>

<p>to check for the proper number of arguments.
</p></dd></dl>


<a name="XREFbeep"></a><dl>
<dt><a name="index-beep"></a>Function File: <em></em> <strong>beep</strong> <em>()</em></dt>
<dd><p>Produce a beep from the speaker (or visual bell).
</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>


<a name="XREFbeep_005fon_005ferror"></a><dl>
<dt><a name="index-beep_005fon_005ferror"></a>Built-in Function: <em><var>val</var> =</em> <strong>beep_on_error</strong> <em>()</em></dt>
<dt><a name="index-beep_005fon_005ferror-1"></a>Built-in Function: <em><var>old_val</var> =</em> <strong>beep_on_error</strong> <em>(<var>new_val</var>)</em></dt>
<dt><a name="index-beep_005fon_005ferror-2"></a>Built-in Function: <em></em> <strong>beep_on_error</strong> <em>(<var>new_val</var>, &quot;local&quot;)</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>&quot;local&quot;</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#Catching-Errors" accesskey="n" rel="next">Catching Errors</a>, Up: <a href="Handling-Errors.html#Handling-Errors" accesskey="u" rel="up">Handling Errors</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>