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


12.2.1 Issuing Warnings

It is possible to issue warnings from any code using the warning function. In its most simple form, the warning function takes a string describing the warning as its input argument. As an example, the following code controls if the variable ‘a’ is non-negative, and if not issues a warning and sets ‘a’ to zero.

a = -1;
if (a < 0)
  warning ("'a' must be non-negative.  Setting 'a' to zero.");
  a = 0;
endif
     -| 'a' must be non-negative.  Setting 'a' to zero.

Since warnings aren’t fatal to a running program, it is not possible to catch a warning using the try statement or something similar. It is however possible to access the last warning as a string using the lastwarn function.

It is also possible to assign an identification string to a warning. If a warning has such an ID the user can enable and disable this warning as will be described in the next section. To assign an ID to a warning, simply call warning with two string arguments, where the first is the identification string, and the second is the actual warning. Note that warning IDs are in the format "NAMESPACE:WARNING-NAME". The namespace "Octave" is used for Octave’s own warnings. Any other string is available as a namespace for user’s own warnings.

: warning (template, …)
: warning (id, template, …)
: warning ("on", id)
: warning ("off", id)
: warning ("query", id)
: warning ("error", id)
: warning (state, "backtrace")
: warning (state, id, "local")

Display a warning message or control the behavior of Octave’s warning system.

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. A message identifier is of the form "NAMESPACE:WARNING-NAME". Octave’s own warnings use the "Octave" namespace (see warning_ids). The special identifier "all" may be used to set the state of all warnings.

If the first argument is "on" or "off", set the state of a particular warning using the identifier id. If the first argument is "query", query the state of this warning instead. 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. So, for example, the following handles all warnings as errors:

warning ("error");

If the state is "on" or "off" and the third argument is "backtrace", then a stack trace is printed along with the warning message when warnings occur inside function calls. This option is enabled by default.

If the state is "on", "off", or "error" and the third argument is "local", then the warning state will be set temporarily, until the end of the current function. Changes to warning states that are set locally affect the current function and all functions called from the current scope. The previous warning state is restored on return from the current function. The "local" option is ignored if used in the top-level workspace.

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_ids, lastwarn, error.

: [msg, msgid] = lastwarn ()
: lastwarn (msg)
: lastwarn (msg, msgid)

Query or set the last warning message.

When called without input arguments, return the last warning message and message identifier.

With one argument, set the last warning message to msg.

With two arguments, also set the last message identifier.

See also: warning, lasterror, lasterr.

The functions distributed with Octave can issue one of the following warnings.

Octave:abbreviated-property-match

By default, the Octave:abbreviated-property-match warning is enabled.

Octave:array-as-logical

If the Octave:array-as-logical warning is enabled, Octave will warn when an array of size greater than 1x1 is used as a truth value in an if, while or until statement. By default, the Octave:array-as-logical warning is disabled.

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:autoload-relative-file-name

If the Octave:autoload-relative-file-name is enabled, Octave will warn when parsing autoload() function calls with relative paths to function files. This usually happens when using autoload() calls in PKG_ADD files, when the PKG_ADD file is not in the same directory as the .oct file referred to by the autoload() command. By default, the Octave:autoload-relative-file-name warning is enabled.

Octave:built-in-variable-assignment

By default, the Octave:built-in-variable-assignment warning is enabled.

Octave:deprecated-function

If the Octave:deprecated-function warning is enabled, a warning is issued when Octave encounters a function that is obsolete and scheduled for removal from Octave. By default, the Octave:deprecated-function warning is enabled.

Octave:deprecated-keyword

If the Octave:deprecated-keyword warning is enabled, a warning is issued when Octave encounters a keyword that is obsolete and scheduled for removal from Octave. By default, the Octave:deprecated-keyword warning is enabled.

Octave:deprecated-property

If the Octave:deprecated-property warning is enabled, a warning is issued when Octave encounters a graphics property that is obsolete and scheduled for removal from Octave. By default, the Octave:deprecated-property 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:fopen-file-in-path

By default, the Octave:fopen-file-in-path warning is enabled.

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

By default, the Octave:glyph-render 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:language-extension

Print warnings when using features that are unique to the Octave language and that may still be missing in MATLAB. By default, the Octave:language-extension warning is disabled. The --traditional or --braindead startup options for Octave may also be of use, see Command Line Options.

Octave:load-file-in-path

By default, the Octave:load-file-in-path warning is enabled.

Octave:logical-conversion

By default, the Octave:logical-conversion warning is enabled.

Octave:missing-glyph

By default, the Octave:missing-glyph warning is enabled.

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:mixed-string-concat

If the Octave:mixed-string-concat warning is enabled, print a warning when concatenating a mixture of double and single quoted strings. By default, the Octave:mixed-string-concat 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:nested-functions-coerced

By default, the Octave:nested-functions-coerced warning is enabled.

Octave:noninteger-range-as-index

By default, the Octave:noninteger-range-as-index warning is enabled.

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:possible-matlab-short-circuit-operator

If the Octave:possible-matlab-short-circuit-operator warning is enabled, Octave will warn about using the not short circuiting operators & and | inside if or while conditions. They normally never short circuit, but MATLAB always short circuits if any logical operators are used in a condition. You can turn on the option

do_braindead_shortcircuit_evaluation (1)

if you would like to enable this short-circuit evaluation in Octave. Note that the && and || operators always short circuit in both Octave and MATLAB, so it’s only necessary to enable MATLAB-style short-circuiting if it’s too arduous to modify existing code that relies on this behavior. By default, the Octave:possible-matlab-short-circuit-operator 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:recursive-path-search

By default, the Octave:recursive-path-search warning is enabled.

Octave:remove-init-dir

The path function changes the search path that Octave uses to find functions. It is possible to set the path to a value which excludes Octave’s own built-in functions. If the Octave:remove-init-dir warning is enabled then Octave will warn when the path function has been used in a way that may render Octave unworkable. By default, the Octave:remove-init-dir 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. By default, the Octave:separator-insert warning is disabled.

Octave:shadowed-function

By default, the Octave:shadowed-function warning is enabled.

Octave:single-quote-string

Print warning if a single quote character is used to introduce a string constant. By default, the Octave:single-quote-string warning is disabled.

Octave:nearly-singular-matrix
Octave:singular-matrix

By default, the Octave:nearly-singular-matrix and Octave:singular-matrix warnings are enabled.

Octave:sqrtm:SingularMatrix

By default, the Octave:sqrtm:SingularMatrix warning is enabled.

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


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