Next: , Up: Handling Errors   [Contents][Index]


12.1.1 Raising Errors

The most common use of errors is for checking input arguments to functions. The following example calls the error function if the function f is called without any input arguments.

function f (arg1)
  if (nargin == 0)
    error ("not enough input arguments");
  endif
endfunction

When the error function is called, it prints the given message and returns to the Octave prompt. This means that no code following a call to error will be executed.

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 error 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 "NAMESPACE:ERROR-NAME". The namespace "Octave" is used for Octave’s own errors. Any other string is available as a namespace for user’s own errors.

: error (template, …)
: error (id, template, …)

Display an error message and stop m-file execution.

Format the optional arguments under the control of the template string template using the same rules as the printf family of functions (see Formatted Output) and print the resulting message on the stderr stream. The message is prefixed by the character string ‘error: ’.

Calling error 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.

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:

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

calling the function f will result in a list of messages that can help you to quickly find the exact location of the error:

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

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:

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

A null string ("") input to error will be ignored and the code will continue running as if the statement were a NOP. This is for compatibility with MATLAB. It also makes it possible to write code such as

err_msg = "";
if (CONDITION 1)
  err_msg = "CONDITION 1 found";
elseif (CONDITION2)
  err_msg = "CONDITION 2 found";
…
endif
error (err_msg);

which will only stop execution if an error has been found.

Implementation Note: For compatibility with MATLAB, escape sequences in template (e.g., "\n" => newline) are processed regardless of whether template 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., "\\n") or use the regexptranslate function.

See also: warning, lasterror.

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 print_usage function is called, it reads the help text of the function calling print_usage, 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 @deftypefn parts of the help text. When the help text isn’t written in Texinfo, the error message contains the entire help message.

Consider the following function.

## -*- texinfo -*-
## @deftypefn {} f (@var{arg1})
## Function help text goes here…
## @end deftypefn
function f (arg1)
  if (nargin == 0)
    print_usage ();
  endif
endfunction

When it is called with no input arguments it produces the following error.

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 http://www.octave.org and via the help@octave.org
-|  mailing list.
: print_usage ()
: print_usage (name)

Print the usage message for the function name.

When called with no input arguments the print_usage function displays the usage message of the currently executing function.

See also: help.

: beep ()

Produce a beep from the speaker (or visual bell).

This function sends the alarm character "\a" to the terminal. Depending on the user’s configuration this may produce an audible beep, a visual bell, or nothing at all.

See also: puts, fputs, printf, fprintf.

: val = beep_on_error ()
: old_val = beep_on_error (new_val)
: beep_on_error (new_val, "local")

Query or set the internal variable that controls whether Octave will try to ring the terminal bell before printing an error message.

When called from inside a function with the "local" option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function.


Next: , Up: Handling Errors   [Contents][Index]