Next: , Previous: Functions and Scripts, Up: Top


13 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
     

— Function File: beep ()

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

     
     
See also: puts, fputs, printf, fprintf.

— 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 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.

— Loadable Function: print_usage ()

Print the usage message for the currently executing function. The print_usage function is only intended to work inside a user-defined function.

     
     
See also: help.

— 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: err = lasterror (err)
— Built-in Function: lasterror ('reset')

Returns or sets the last error message. Called without any arguments returns a structure containing the last error message, as well as other information related to this error. The elements of this structure are:

'message'
The text of the last error message
'identifier'
The message identifier of this error message
'stack'
A structure containing information on where the message occured. This might be an empty structure if this in the case where this information can not be obtained. The fields of this structure are:
'file'
The name of the file where the error occurred
'name'
The name of function in which the error occured
'line'
The line number at which the error occured
'column'
An optional field with the column number at which the error occurred

The err structure may also be passed to lasterror to set the information about the last error. The only constraint on err in that case is that it is a scalar structure. Any fields of err that match the above are set to the value passed in err, while other fields are set to their default values.

If lasterror is called with the argument 'reset', all values take their default values.

— 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.

— Built-in Function: rethrow (err)

Reissues a previous error as defined by err. err is a structure that must contain at least the 'message' and 'identifier' fields. err can also contain a field 'stack' that gives information on the assumed location of the error. Typically err is returned from lasterror.

     
     
See also: lasterror, lasterr, error.

Octave:array-to-scalar
If the Octave:array-to-scalar warning is enabled, Octave will warn when an implicit conversion from an array to a scalar value is attempted. By default, the Octave:array-to-scalar warning is disabled.
Octave:array-to-vector
If the Octave:array-to-vector warning is enabled, Octave will warn when an implicit conversion from an array to a vector value is attempted. By default, the Octave:array-to-vector warning is disabled.
Octave:assign-as-truth-value
If the Octave:assign-as-truth-value warning is enabled, a warning is issued for statements like
          if (s = t)
            ...
     

since such statements are not common, and it is likely that the intent was to write

          if (s == t)
            ...
     

instead.

There are times when it is useful to write code that contains assignments within the condition of a while or if statement. For example, statements like

          while (c = getc())
            ...
     

are common in C programming.

It is possible to avoid all warnings about such statements by disabling the Octave:assign-as-truth-value warning, but that may also let real errors like

          if (x = 1)  # intended to test (x == 1)!
            ...
     

slip by.

In such cases, it is possible suppress errors for specific statements by writing them with an extra set of parentheses. For example, writing the previous example as

          while ((c = getc()))
            ...
     

will prevent the warning from being printed for this statement, while allowing Octave to warn about other assignments used in conditional contexts.

By default, the Octave:assign-as-truth-value warning is enabled.

Octave:associativity-change
If the Octave:associativity-change warning is enabled, Octave will warn about possible changes in the meaning of some code due to changes in associativity for some operators. Associativity changes have typically been made for Matlab compatibility. By default, the Octave:associativity-change warning is enabled.
Octave:divide-by-zero
If the Octave:divide-by-zero warning is enabled, a warning is issued when Octave encounters a division by zero. By default, the Octave:divide-by-zero warning is enabled.
Octave:empty-list-elements
If the Octave:empty-list-elements warning is enabled, a warning is issued when an empty matrix is found in a matrix list. For example,
          a = [1, [], 3, [], 5]
     

By default, the Octave:empty-list-elements warning is enabled.

Octave:fortran-indexing
If the Octave:fortran-indexing warning is enabled, a warning is printed for expressions which select elements of a two-dimensional matrix using a single index. By default, the Octave:fortran-indexing warning is disabled.
Octave:function-name-clash
If the Octave:function-name-clash warning is enabled, a warning is issued when Octave finds that the name of a function defined in a function file differs from the name of the file. (If the names disagree, the name declared inside the file is ignored.) By default, the Octave:function-name-clash warning is enabled.
Octave:future-time-stamp
If the Octave:future-time-stamp warning is enabled, Octave will print a warning if it finds a function file with a time stamp that is in the future. By default, the Octave:future-time-stamp warning is enabled.
Octave:imag-to-real
If the Octave:imag-to-real warning is enabled, a warning is printed for implicit conversions of complex numbers to real numbers. By default, the Octave:imag-to-real warning is disabled.
Octave:matlab-incompatible
Print warnings for Octave language features that may cause compatibility problems with Matlab.
Octave:missing-semicolon
If the Octave:missing-semicolon warning is enabled, Octave will warn when statements in function definitions don't end in semicolons. By default the Octave:missing-semicolon warning is disabled.
Octave:neg-dim-as-zero
If the Octave:neg-dim-as-zero warning is enabled, print a warning for expressions like
          eye (-1)
     

By default, the Octave:neg-dim-as-zero warning is disabled.

Octave:num-to-str
If the Octave:num-to-str warning is enable, a warning is printed for implicit conversions of numbers to their ASCII character equivalents when strings are constructed using a mixture of strings and numbers in matrix notation. For example,
          [ "f", 111, 111 ]
          => "foo"
     

elicits a warning if the Octave:num-to-str warning is enabled. By default, the Octave:num-to-str warning is enabled.

Octave:precedence-change
If the Octave:precedence-change warning is enabled, Octave will warn about possible changes in the meaning of some code due to changes in precedence for some operators. Precedence changes have typically been made for Matlab compatibility. By default, the Octave:precedence-change warning is enabled.
Octave:reload-forces-clear
If several functions have been loaded from the same file, Octave must clear all the functions before any one of them can be reloaded. If the Octave:reload-forces-clear warning is enabled, Octave will warn you when this happens, and print a list of the additional functions that it is forced to clear. By default, the Octave:reload-forces-clear warning is enabled.
Octave:resize-on-range-error
If the Octave:resize-on-range-error warning is enabled, print a warning when a matrix is resized by an indexed assignment with indices outside the current bounds. By default, the Octave:resize-on-range-error warning is disabled.
Octave:separator-insert
Print warning if commas or semicolons might be inserted automatically in literal matrices.
Octave:single-quote-string
Print warning if a signle quote character is used to introduce a string constant.
Octave:str-to-num
If the Octave:str-to-num warning is enabled, a warning is printed for implicit conversions of strings to their numeric ASCII equivalents. For example,
          "abc" + 0
          => 97 98 99
     

elicits a warning if the Octave:str-to-num warning is enabled. By default, the Octave:str-to-num warning is disabled.

Octave:string-concat
If the Octave:string-concat warning is enabled, print a warning when concatenating a mixture of double and single quoted strings. By default, the Octave:string-concat warning is disabled.
Octave:undefined-return-values
If the Octave:undefined-return-values warning is disabled, print a warning if a function does not define all the values in the return list which are expected. By default, the Octave:undefined-return-values warning is enabled.
Octave:variable-switch-label
If the Octave:variable-switch-label warning is enabled, Octave will print a warning if a switch label is not a constant or constant expression. By default, the Octave:variable-switch-label warning is disabled.

— Built-in Function: err = errno ()
— Built-in Function: err = errno (val)
— Built-in Function: err = errno (name)

Return the current value of the system-dependent variable errno, set its value to val and return the previous value, or return the named error code given name as a character string, or -1 if name is not found.

— Built-in Function: errno_list ()

Return a structure containing the system-dependent errno values.

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.