@c DO NOT EDIT!  Generated automatically by munge-texi.

@c Copyright (C) 1996, 1997 John W. Eaton
@c This is part of the Octave manual.
@c For copying conditions, see the file gpl.texi.

@node Error Handling
@chapter Error Handling

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.

@anchor{doc-error}
@deftypefn {Built-in Function} {} error (@var{template}, @dots{})
The @code{error} function formats the optional arguments under the
control of the template string @var{template} using the same rules as
the @code{printf} family of functions (@pxref{Formatted Output}).
The resulting message is prefixed by the string @samp{error: } and
printed on the @code{stderr} stream.

Calling @code{error} 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.

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:

@example
@group
function f () g () end
function g () h () end
function h () nargin == 1 || error ("nargin != 1"); end
@end group
@end example

@noindent
calling the function @code{f} will result in a list of messages that
can help you to quickly locate the exact location of the error:

@example
@group
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'
@end group
@end example

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:

@example
@group
function h () nargin == 1 || error ("nargin != 1\n"); end
f ()
error: nargin != 1
@end group
@end example
@end deftypefn


@anchor{doc-beep_on_error}
@defvr {Built-in Variable} beep_on_error
If the value of @code{beep_on_error} is nonzero, Octave will try
to ring your terminal's bell before printing an error message.  The
default value is 0.
@end defvr


@anchor{doc-warning}
@deftypefn {Built-in Function} {} warning (@var{msg})
Print a warning message @var{msg} prefixed by the string @samp{warning: }.  
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.
@end deftypefn


@anchor{doc-usage}
@deftypefn {Built-in Function} {} usage (@var{msg})
Print the message @var{msg}, prefixed by the string @samp{usage: }, 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.

After @code{usage} is evaluated, Octave will print a traceback of all
the function calls leading to the usage message.

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

@example
@group
if (nargin != 2)
  usage ("foo (a, b)");
endif
@end group
@end example

@noindent
to check for the proper number of arguments.
@end deftypefn


@anchor{doc-lasterr}
@deftypefn {Built-in Function} {} lasterr ()
@deftypefnx {Built-in Function} {} lasterr (@var{msg})
Without any arguments, return the last error message.  With one
argument, set the last warning message to @var{msg}.
@end deftypefn


@anchor{doc-lastwarn}
@deftypefn {Built-in Function} {} lastwarn ()
@deftypefnx {Built-in Function} {} lastwarn (@var{msg})
Without any arguments, return the last warning message.  With one
argument, set the last error message to @var{msg}.
@end deftypefn


The following pair of functions are of limited usefulness, and may be
removed from future versions of Octave.

@anchor{doc-perror}
@deftypefn {Function File} {} perror (@var{name}, @var{num})
Print the error message for function @var{name} corresponding to the
error number @var{num}.  This function is intended to be used to print
useful error messages for those functions that return numeric error
codes.
@end deftypefn
@seealso{strerror}


@anchor{doc-strerror}
@deftypefn {Function File} {} strerror (@var{name}, @var{num})
Return the text of an error message for function @var{name}
corresponding to the error number @var{num}.  This function is intended
to be used to print useful error messages for those functions that
return numeric error codes.
@end deftypefn

