## 14 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.

— Built-in Function: error (template, ...)

> 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 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:

>               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 locate the exact location of the error:

>               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'

> 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:

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

— Built-in Function: val = beep_on_error ()
— Built-in Function: old_val = beep_on_error (new_val)

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

— Built-in Function: warning (template, ...)
— Built-in Function: warning (id, template)

> 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 ``warning:` '. 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.

> The optional message identifier allows users to enable or disable warnings tagged by id. The special identifier ``"all"`' may be used to set the state of all warnings. — Built-in Function: warning ("on", id)
> — Built-in Function: warning ("off", id)
> — Built-in Function: warning ("error", id)
> — Built-in Function: warning ("query", id)

> > Set or query the the state of a particular warning using the identifier id. If the identifier is omitted, a value of ``"all"`' is assumed. If you set the state of a warning to ``"error"`', the warning named by id is handled as if it were an error instead.

> > See also: warning_ids.

— Built-in Function: usage (msg)

> Print the message msg, prefixed by the string ``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 `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

>               if (nargin != 2)
>                 usage ("foo (a, b)");
>               endif

> to check for the proper number of arguments.

— Built-in Function: [msg, msgid] = lasterr (msg, msgid)

> Without any arguments, return the last error message. With one argument, set the last error message to msg. With two arguments, also set the last message identifier.

— Built-in Function: [msg, msgid] = lastwarn (msg, msgid)

> Without any arguments, return the last warning message. With one argument, set the last warning message to msg. With two arguments, also set the last message identifier.

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

— Function File: perror (name, num)

> Print the error message for function name corresponding to the error number num. This function is intended to be used to print useful error messages for those functions that return numeric error codes.

> See also: strerror.

— Function File: strerror (name, num)

> Return the text of an error message for function name corresponding to the error number num. This function is intended to be used to print useful error messages for those functions that return numeric error codes.