Continuo da qui finendo il capitolo errori, qui.
Gestire i warnings
Like an error, a warning is issued when something unexpected happens. Unlike an error, a warning doesn’t abort the currently running program. A simple example of a warning is when a number is divided by zero. In this case Octave will issue a warning and assign the value Inf
to the result.
Passo qui.
Segnalare un warning
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.
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 ID
s 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.
Built-in Function: warning (template, ...)
Built-in Function: warning (id, template, ...)
Built-in Function: warning ("on", id)
Built-in Function: warning ("off", id)
Built-in Function: warning ("query", id)
Built-in Function: warning ("error", id)
Built-in Function: warning (state, "backtrace")
Built-in Function: 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 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 XREFwarning_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.
Built-in Function: [msg, msgid] = lastwarn ()
Built-in Function: lastwarn (msg)
Built-in Function: 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.
The functions distributed with Octave can issue one of the following warnings. Non la riporto, è lunga, inutile (quasi) e poi c’è di là dovesse servire.
Avanti, qui.
Abilitare e disabilitare i warnings
The warning function also allows you to control which warnings are actually printed to the screen. If the warning function is called with a string argument that is either "on"
or "off"
all warnings will be enabled or disabled.
It is also possible to enable and disable individual warnings through their string identifications. Example:
Nota perso: io appartengo alla scuola che errori e, in misura minore) warnings vanno presi con molta considerazione. E corretti senza proseguire l’elaborazione.
Aneddoto fine anni ’70: il programma per la soluzione di telai che usavamo a volte abortiva con un messaggio incomprensibile. La prassi era quella di cancellare il file temporaneo T$0000
, controllare i dati di input e rilanciare il programma. Quando ho provato a chiarire il messaggio di errore ho scoperto che il file temporaneo conteneva il dato errato. Resta il mystero mysteryouso a tutt’oggi perché il programmatore avesse voluto scrivere su file invece che a video e non l’avesse scritto sul manuale d’uso 😳