<html lang="en"> <head> <title>Issuing Warnings - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Handling-Warnings.html#Handling-Warnings" title="Handling Warnings"> <link rel="next" href="Enabling-and-Disabling-Warnings.html#Enabling-and-Disabling-Warnings" title="Enabling and Disabling Warnings"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Issuing-Warnings"></a> <p> Next: <a rel="next" accesskey="n" href="Enabling-and-Disabling-Warnings.html#Enabling-and-Disabling-Warnings">Enabling and Disabling Warnings</a>, Up: <a rel="up" accesskey="u" href="Handling-Warnings.html#Handling-Warnings">Handling Warnings</a> <hr> </div> <h4 class="subsection">12.2.1 Issuing Warnings</h4> <p>It is possible to issue warnings from any code using the <code>warning</code> function. In its most simple form, the <code>warning</code> function takes a string describing the warning as its input argument. As an example, the following code controls if the variable ‘<samp><span class="samp">a</span></samp>’ is non-negative, and if not issues a warning and sets ‘<samp><span class="samp">a</span></samp>’ to zero. <pre class="example"> 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. </pre> <p>Since warnings aren't fatal to a running program, it is not possible to catch a warning using the <code>try</code> statement or something similar. It is however possible to access the last warning as a string using the <code>lastwarn</code> function. <p>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 <code>warning</code> 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 src/error.cc --> <p><a name="doc_002dwarning"></a> <div class="defun"> — Built-in Function: <b>warning</b> (<var>template, <small class="dots">...</small></var>)<var><a name="index-warning-822"></a></var><br> — Built-in Function: <b>warning</b> (<var>id, template, <small class="dots">...</small></var>)<var><a name="index-warning-823"></a></var><br> — Built-in Function: <b>warning</b> (<var>"on", id</var>)<var><a name="index-warning-824"></a></var><br> — Built-in Function: <b>warning</b> (<var>"off", id</var>)<var><a name="index-warning-825"></a></var><br> — Built-in Function: <b>warning</b> (<var>"query", id</var>)<var><a name="index-warning-826"></a></var><br> — Built-in Function: <b>warning</b> (<var>"error", id</var>)<var><a name="index-warning-827"></a></var><br> <blockquote><p>Format the optional arguments under the control of the template string <var>template</var> using the same rules as the <code>printf</code> family of functions (see <a href="Formatted-Output.html#Formatted-Output">Formatted Output</a>) and print the resulting message on the <code>stderr</code> stream. The message is prefixed by the character string ‘<samp><span class="samp">warning: </span></samp>’. 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. <p>The optional message identifier allows users to enable or disable warnings tagged by <var>id</var>. A message identifier is of the form "NAMESPACE:WARNING-NAME". Octave's own warnings use the "Octave" namespace (see <a href="doc_002dwarning_005fids.html#doc_002dwarning_005fids">doc-warning_ids</a>). The special identifier ‘<samp><span class="samp">"all"</span></samp>’ may be used to set the state of all warnings. <p>If the first argument is ‘<samp><span class="samp">"on"</span></samp>’ or ‘<samp><span class="samp">"off"</span></samp>’, set the state of a particular warning using the identifier <var>id</var>. If the first argument is ‘<samp><span class="samp">"query"</span></samp>’, query the state of this warning instead. If the identifier is omitted, a value of ‘<samp><span class="samp">"all"</span></samp>’ is assumed. If you set the state of a warning to ‘<samp><span class="samp">"error"</span></samp>’, the warning named by <var>id</var> is handled as if it were an error instead. So, for example, the following handles all warnings as errors: <pre class="example"> warning ("error"); </pre> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dwarning_005fids.html#doc_002dwarning_005fids">warning_ids</a>. </p></blockquote></div> <!-- lastwarn src/error.cc --> <p><a name="doc_002dlastwarn"></a> <div class="defun"> — Built-in Function: [<var>msg</var>, <var>msgid</var>] = <b>lastwarn</b> (<var>msg, msgid</var>)<var><a name="index-lastwarn-828"></a></var><br> <blockquote><p>Without any arguments, return the last warning message. With one argument, set the last warning message to <var>msg</var>. With two arguments, also set the last message identifier. </p></blockquote></div> </body></html>