It is possible to issue warnings from any code using the
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
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,
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.
Format the optional arguments under the control of the template string template using the same rules as the
printffamily of functions (see Formatted Output) and print the resulting message on the
stderrstream. 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 doc-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");
See also: warning_ids.