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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html40/loose.dtd">
<HTML>
<!-- Created on May, 18 2005 by texi2html 1.66 -->
<!--
Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author)
Karl Berry <karl@freefriends.org>
Olaf Bachmann <obachman@mathematik.uni-kl.de>
and many others.
Maintained by: Many creative people <dev@texi2html.cvshome.org>
Send bugs and suggestions to <users@texi2html.cvshome.org>
-->
<HEAD>
<TITLE>GNU Octave: Error Handling</TITLE>
<META NAME="description" CONTENT="GNU Octave: Error Handling">
<META NAME="keywords" CONTENT="GNU Octave: Error Handling">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META NAME="Generator" CONTENT="texi2html 1.66">
</HEAD>
<BODY LANG="en" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000">
<A NAME="SEC111"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_14.html#SEC110"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_16.html#SEC112"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_14.html#SEC98"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_16.html#SEC112"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_42.html#SEC245">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H1> 14. Error Handling </H1>
<!--docid::SEC111::-->
<P>
Octave includes several functions for printing error and warning
messages. When you write functions that need to take special action
when they encounter abnormal conditions, you should print the error
messages using the functions described in this chapter.
</P>
<P>
<A NAME="doc-error"></A>
<A NAME="IDX393"></A>
</P>
<DL>
<DT><U>Built-in Function:</U> <B>error</B> <I>(<VAR>template</VAR>, <small>...</small>)</I>
<DD>The <CODE>error</CODE> function formats 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 section <A HREF="octave_17.html#SEC122">16.2.4 Formatted Output</A>).
The resulting message is prefixed by the string `<SAMP>error: </SAMP>' and
printed on the <CODE>stderr</CODE> stream.
<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>
<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>
<P>
<TABLE><tr><td> </td><td class=example><pre>function f () g () end
function g () h () end
function h () nargin == 1 || error ("nargin != 1"); end
</pre></td></tr></table><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>
<P>
<TABLE><tr><td> </td><td class=example><pre>f ()
error: nargin != 1
error: evaluating index expression near line 1, column 30
error: evaluating binary operator `||' near line 1, column 27
error: called from `h'
error: called from `g'
error: called from `f'
</pre></td></tr></table><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>
<P>
<TABLE><tr><td> </td><td class=example><pre>function h () nargin == 1 || error ("nargin != 1\n"); end
f ()
error: nargin != 1
</pre></td></tr></table></DL>
<P>
<A NAME="doc-beep_on_error"></A>
<A NAME="IDX394"></A>
</P>
<DL>
<DT><U>Built-in Variable:</U> <B>beep_on_error</B>
<DD>If the value of <CODE>beep_on_error</CODE> is nonzero, Octave will try
to ring your terminal's bell before printing an error message. The
default value is 0.
</DL>
<P>
<A NAME="doc-warning"></A>
<A NAME="IDX395"></A>
</P>
<DL>
<DT><U>Built-in Function:</U> <B>warning</B> <I>(<VAR>msg</VAR>)</I>
<DD>Print a warning message <VAR>msg</VAR> prefixed by the string `<SAMP>warning: </SAMP>'.
After printing the warning message, Octave will continue to execute
commands. You should use this function when you want to notify the user
of an unusual condition, but only when it makes sense for your program
to go on.
</DL>
<P>
<A NAME="doc-usage"></A>
<A NAME="IDX396"></A>
</P>
<DL>
<DT><U>Built-in Function:</U> <B>usage</B> <I>(<VAR>msg</VAR>)</I>
<DD>Print the message <VAR>msg</VAR>, prefixed by the string `<SAMP>usage: </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>
<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>
<P>
<TABLE><tr><td> </td><td class=example><pre>if (nargin != 2)
usage ("foo (a, b)");
endif
</pre></td></tr></table><P>
to check for the proper number of arguments.
</P>
</DL>
<P>
<A NAME="doc-lasterr"></A>
<A NAME="IDX397"></A>
</P>
<DL>
<DT><U>Built-in Function:</U> <B>lasterr</B> <I>()</I>
<DD><A NAME="IDX398"></A>
<DT><U>Built-in Function:</U> <B>lasterr</B> <I>(<VAR>msg</VAR>)</I>
<DD>Without any arguments, return the last error message. With one
argument, set the last error message to <VAR>msg</VAR>.
</DL>
<P>
<A NAME="doc-lastwarn"></A>
<A NAME="IDX399"></A>
</P>
<DL>
<DT><U>Built-in Function:</U> <B>lastwarn</B> <I>()</I>
<DD><A NAME="IDX400"></A>
<DT><U>Built-in Function:</U> <B>lastwarn</B> <I>(<VAR>msg</VAR>)</I>
<DD>Without any arguments, return the last warning message. With one
argument, set the last warning message to <VAR>msg</VAR>.
</DL>
<P>
The following pair of functions are of limited usefulness, and may be
removed from future versions of Octave.
</P>
<P>
<A NAME="doc-perror"></A>
<A NAME="IDX401"></A>
</P>
<DL>
<DT><U>Function File:</U> <B>perror</B> <I>(<VAR>name</VAR>, <VAR>num</VAR>)</I>
<DD>Print the error message for function <VAR>name</VAR> corresponding to the
error number <VAR>num</VAR>. This function is intended to be used to print
useful error messages for those functions that return numeric error
codes.
</DL>
<P>
<A NAME="doc-strerror"></A>
<A NAME="IDX402"></A>
</P>
<DL>
<DT><U>Function File:</U> <B>strerror</B> <I>(<VAR>name</VAR>, <VAR>num</VAR>)</I>
<DD>Return the text of an error message for function <VAR>name</VAR>
corresponding to the error number <VAR>num</VAR>. This function is intended
to be used to print useful error messages for those functions that
return numeric error codes.
</DL>
<P>
<A NAME="Debugging"></A>
<HR SIZE="6">
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_14.html#SEC98"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_16.html#SEC112"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_42.html#SEC245">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<BR>
<FONT SIZE="-1">
This document was generated
by <I>John W. Eaton</I> on <I>May, 18 2005</I>
using <A HREF="http://texi2html.cvshome.org"><I>texi2html</I></A>
</FONT>
</BODY>
</HTML>
|