1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
<html lang="en">
<head>
<title>Issuing Warnings - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.11">
<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">
<p>
<a name="Issuing-Warnings"></a>
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.
<!-- 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-678"></a></var><br>
— Built-in Function: <b>warning</b> (<var>id, template, <small class="dots">...</small></var>)<var><a name="index-warning-679"></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>. The special identifier ‘<samp><span class="samp">"all"</span></samp>’ may
be used to set the state of all warnings.
— Built-in Function: <b>warning</b> (<var>"on", id</var>)<var><a name="index-warning-680"></a></var><br>
— Built-in Function: <b>warning</b> (<var>"off", id</var>)<var><a name="index-warning-681"></a></var><br>
— Built-in Function: <b>warning</b> (<var>"error", id</var>)<var><a name="index-warning-682"></a></var><br>
— Built-in Function: <b>warning</b> (<var>"query", id</var>)<var><a name="index-warning-683"></a></var><br>
<blockquote><p>Set or query the state of a particular warning using the identifier
<var>id</var>. 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.
<!-- 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>
<!-- 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-684"></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>
|