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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
|
@c DO NOT EDIT! Generated automatically by munge-texi.
@c Copyright (C) 1996, 1997, 2007, 2008, 2009 John W. Eaton
@c
@c This file is part of Octave.
@c
@c Octave is free software; you can redistribute it and/or modify it
@c under the terms of the GNU General Public License as published by the
@c Free Software Foundation; either version 3 of the License, or (at
@c your option) any later version.
@c
@c Octave is distributed in the hope that it will be useful, but WITHOUT
@c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
@c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
@c for more details.
@c
@c You should have received a copy of the GNU General Public License
@c along with Octave; see the file COPYING. If not, see
@c <http://www.gnu.org/licenses/>.
@node Errors and Warnings
@chapter Errors and Warnings
Octave includes several functions for printing error and warning
messages. When you write functions that need to take special action
when they encounter abnormal conditions, you should print the error
messages using the functions described in this chapter.
Since many of Octave's functions use these functions, it is also useful
to understand them, so that errors and warnings can be handled.
@menu
* Handling Errors::
* Handling Warnings::
@end menu
@node Handling Errors
@section Handling Errors
An error is something that occurs when a program is in a state where
it doesn't make sense to continue. An example is when a function is
called with too few input arguments. In this situation the function
should abort with an error message informing the user of the lacking
input arguments.
Since an error can occur during the evaluation of a program, it is
very convenient to be able to detect that an error occurred, so that
the error can be fixed. This is possible with the @code{try} statement
described in @ref{The @code{try} Statement}.
@menu
* Raising Errors::
* Catching Errors::
@end menu
@node Raising Errors
@subsection Raising Errors
The most common use of errors is for checking input arguments to
functions. The following example calls the @code{error} function if
the function @code{f} is called without any input arguments.
@example
@group
function f (arg1)
if (nargin == 0)
error("not enough input arguments");
endif
endfunction
@end group
@end example
When the @code{error} function is called, it prints the given message
and returns to the Octave prompt. This means that no code following
a call to @code{error} will be executed.
@c error.cc
@anchor{doc-error}
@deftypefn {Built-in Function} {} error (@var{template}, @dots{})
@deftypefnx {Built-in Function} {} error (@var{id}, @var{template}, @dots{})
Format the optional arguments under the control of the template string
@var{template} using the same rules as the @code{printf} family of
functions (@pxref{Formatted Output}) and print the resulting message
on the @code{stderr} stream. The message is prefixed by the character
string @samp{error: }.
Calling @code{error} also sets Octave's internal error state such that
control will return to the top level without evaluating any more
commands. This is useful for aborting from functions or scripts.
If the error message does not end with a new line character, Octave will
print a traceback of all the function calls leading to the error. For
example, given the following function definitions:
@example
@group
function f () g (); end
function g () h (); end
function h () nargin == 1 || error ("nargin != 1"); end
@end group
@end example
@noindent
calling the function @code{f} will result in a list of messages that
can help you to quickly locate the exact location of the error:
@example
@group
f ()
error: nargin != 1
error: called from:
error: error at line -1, column -1
error: h at line 1, column 27
error: g at line 1, column 15
error: f at line 1, column 15
@end group
@end example
If the error message ends in a new line character, Octave will print the
message but will not display any traceback messages as it returns
control to the top level. For example, modifying the error message
in the previous example to end in a new line causes Octave to only print
a single message:
@example
@group
function h () nargin == 1 || error ("nargin != 1\n"); end
f ()
error: nargin != 1
@end group
@end example
@end deftypefn
Since it is common to use errors when there is something wrong with
the input to a function, Octave supports functions to simplify such code.
When the @code{print_usage} function is called, it reads the help text
of the function calling @code{print_usage}, and presents a useful error.
If the help text is written in Texinfo it is possible to present an
error message that only contains the function prototypes as described
by the @code{@@deftypefn} parts of the help text. When the help text
isn't written in Texinfo, the error message contains the entire help
message.
Consider the following function.
@example
@group
## -*- texinfo -*-
## @@deftypefn @{Function File@} f (@@var@{arg1@})
## Function help text goes here@dots{}
## @@end deftypefn
function f (arg1)
if (nargin == 0)
print_usage ();
endif
endfunction
@end group
@end example
@noindent
When it is called with no input arguments it produces the following
error.
@example
@group
f ()
@print{} error: Invalid call to f. Correct usage is:
@print{}
@print{} -- Function File: f (ARG1)
@print{}
@print{}
@print{} Additional help for built-in functions and operators is
@print{} available in the on-line version of the manual. Use the command
@print{} `doc <topic>' to search the manual index.
@print{}
@print{} Help and information about Octave is also available on the WWW
@print{} at http://www.octave.org and via the help@@octave.org
@print{} mailing list.
@end group
@end example
@c ./help/print_usage.m
@anchor{doc-print_usage}
@deftypefn {Function File} {} print_usage ()
@deftypefnx{Function File} {} print_usage (@var{name})
Print the usage message for a function. When called with no input arguments
the @code{print_usage} function displays the usage message of the currently
executing function.
@seealso{@ref{doc-help,,help}}
@end deftypefn
@c error.cc
@anchor{doc-usage}
@deftypefn {Built-in Function} {} usage (@var{msg})
Print the message @var{msg}, prefixed by the string @samp{usage: }, and
set Octave's internal error state such that control will return to the
top level without evaluating any more commands. This is useful for
aborting from functions.
After @code{usage} is evaluated, Octave will print a traceback of all
the function calls leading to the usage message.
You should use this function for reporting problems errors that result
from an improper call to a function, such as calling a function with an
incorrect number of arguments, or with arguments of the wrong type. For
example, most functions distributed with Octave begin with code like
this
@example
@group
if (nargin != 2)
usage ("foo (a, b)");
endif
@end group
@end example
@noindent
to check for the proper number of arguments.
@end deftypefn
@c ./io/beep.m
@anchor{doc-beep}
@deftypefn {Function File} {} beep ()
Produce a beep from the speaker (or visual bell).
@seealso{@ref{doc-puts,,puts}, @ref{doc-fputs,,fputs}, @ref{doc-printf,,printf}, @ref{doc-fprintf,,fprintf}}
@end deftypefn
@c error.cc
@anchor{doc-beep_on_error}
@deftypefn {Built-in Function} {@var{val} =} beep_on_error ()
@deftypefnx {Built-in Function} {@var{old_val} =} beep_on_error (@var{new_val})
Query or set the internal variable that controls whether Octave will try
to ring the terminal bell before printing an error message.
@end deftypefn
@node Catching Errors
@subsection Catching Errors
When an error occurs, it can be detected and handled using the
@code{try} statement as described in @ref{The @code{try} Statement}.
As an example, the following piece of code counts the number of errors
that occurs during a @code{for} loop.
@example
@group
number_of_errors = 0;
for n = 1:100
try
@dots{}
catch
number_of_errors++;
end_try_catch
endfor
@end group
@end example
The above example treats all errors the same. In many situations it
can however be necessary to discriminate between errors, and take
different actions depending on the error. The @code{lasterror}
function returns a structure containing information about the last
error that occurred. As an example, the code above could be changed
to count the number of errors related to the @samp{*} operator.
@example
@group
number_of_errors = 0;
for n = 1:100
try
@dots{}
catch
msg = lasterror.message;
if (strfind (msg, "operator *"))
number_of_errors++;
endif
end_try_catch
endfor
@end group
@end example
@c error.cc
@anchor{doc-lasterror}
@deftypefn {Built-in Function} {@var{err} =} lasterror (@var{err})
@deftypefnx {Built-in Function} {} lasterror ('reset')
Returns or sets the last error message. Called without any arguments
returns a structure containing the last error message, as well as other
information related to this error. The elements of this structure are:
@table @asis
@item 'message'
The text of the last error message
@item 'identifier'
The message identifier of this error message
@item 'stack'
A structure containing information on where the message occurred. This might
be an empty structure if this in the case where this information cannot
be obtained. The fields of this structure are:
@table @asis
@item 'file'
The name of the file where the error occurred
@item 'name'
The name of function in which the error occurred
@item 'line'
The line number at which the error occurred
@item 'column'
An optional field with the column number at which the error occurred
@end table
@end table
The @var{err} structure may also be passed to @code{lasterror} to set the
information about the last error. The only constraint on @var{err} in that
case is that it is a scalar structure. Any fields of @var{err} that match
the above are set to the value passed in @var{err}, while other fields are
set to their default values.
If @code{lasterror} is called with the argument 'reset', all values take
their default values.
@end deftypefn
@c error.cc
@anchor{doc-lasterr}
@deftypefn {Built-in Function} {[@var{msg}, @var{msgid}] =} lasterr (@var{msg}, @var{msgid})
Without any arguments, return the last error message. With one
argument, set the last error message to @var{msg}. With two arguments,
also set the last message identifier.
@end deftypefn
When an error has been handled it is possible to raise it again. This
can be useful when an error needs to be detected, but the program should
still abort. This is possible using the @code{rethrow} function. The
previous example can now be changed to count the number of errors
related to the @samp{*} operator, but still abort if another kind of
error occurs.
@example
@group
number_of_errors = 0;
for n = 1:100
try
@dots{}
catch
msg = lasterror.message;
if (strfind (msg, "operator *"))
number_of_errors++;
else
rethrow (lasterror);
endif
end_try_catch
endfor
@end group
@end example
@c error.cc
@anchor{doc-rethrow}
@deftypefn {Built-in Function} {} rethrow (@var{err})
Reissues a previous error as defined by @var{err}. @var{err} is a structure
that must contain at least the 'message' and 'identifier' fields. @var{err}
can also contain a field 'stack' that gives information on the assumed
location of the error. Typically @var{err} is returned from
@code{lasterror}.
@seealso{@ref{doc-lasterror,,lasterror}, @ref{doc-lasterr,,lasterr}, @ref{doc-error,,error}}
@end deftypefn
@c FIXME: I have no idea what the rest of the functions are used for...
@c utils.cc
@anchor{doc-errno}
@deftypefn {Built-in Function} {@var{err} =} errno ()
@deftypefnx {Built-in Function} {@var{err} =} errno (@var{val})
@deftypefnx {Built-in Function} {@var{err} =} errno (@var{name})
Return the current value of the system-dependent variable errno,
set its value to @var{val} and return the previous value, or return
the named error code given @var{name} as a character string, or -1
if @var{name} is not found.
@end deftypefn
@c utils.cc
@anchor{doc-errno_list}
@deftypefn {Built-in Function} {} errno_list ()
Return a structure containing the system-dependent errno values.
@end deftypefn
@node Handling Warnings
@section Handling 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 @code{Inf}
to the result.
@example
@group
a = 1/0
@print{} warning: division by zero
@result{} a = Inf
@end group
@end example
@menu
* Issuing Warnings::
* Enabling and Disabling Warnings::
@end menu
@node Issuing Warnings
@subsection Issuing Warnings
It is possible to issue warnings from any code using the @code{warning}
function. In its most simple form, the @code{warning} function takes a
string describing the warning as its input argument. As an example,
the following code controls if the variable @samp{a} is non-negative,
and if not issues a warning and sets @samp{a} to zero.
@example
@group
a = -1;
if (a < 0)
warning ("'a' must be non-negative. Setting 'a' to zero.");
a = 0;
endif
@print{} 'a' must be non-negative. Setting 'a' to zero.
@end group
@end example
Since warnings aren't fatal to a running program, it is not possible
to catch a warning using the @code{try} statement or something similar.
It is however possible to access the last warning as a string using the
@code{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 @code{warning} with two string arguments, where the first
is the identification string, and the second is the actual warning.
@c error.cc
@anchor{doc-warning}
@deftypefn {Built-in Function} {} warning (@var{template}, @dots{})
@deftypefnx {Built-in Function} {} warning (@var{id}, @var{template}, @dots{})
Format the optional arguments under the control of the template string
@var{template} using the same rules as the @code{printf} family of
functions (@pxref{Formatted Output}) and print the resulting message
on the @code{stderr} stream. The message is prefixed by the character
string @samp{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 @var{id}. The special identifier @samp{"all"} may
be used to set the state of all warnings.
@deftypefnx {Built-in Function} {} warning ("on", @var{id})
@deftypefnx {Built-in Function} {} warning ("off", @var{id})
@deftypefnx {Built-in Function} {} warning ("error", @var{id})
@deftypefnx {Built-in Function} {} warning ("query", @var{id})
Set or query the state of a particular warning using the identifier
@var{id}. If the identifier is omitted, a value of @samp{"all"} is
assumed. If you set the state of a warning to @samp{"error"}, the
warning named by @var{id} is handled as if it were an error instead.
@seealso{@ref{doc-warning_ids,,warning_ids}}
@end deftypefn
@c error.cc
@anchor{doc-lastwarn}
@deftypefn {Built-in Function} {[@var{msg}, @var{msgid}] =} lastwarn (@var{msg}, @var{msgid})
Without any arguments, return the last warning message. With one
argument, set the last warning message to @var{msg}. With two arguments,
also set the last message identifier.
@end deftypefn
@node Enabling and Disabling Warnings
@subsection Enabling and Disabling Warnings
The @code{warning} function also allows you to control which warnings
are actually printed to the screen. If the @code{warning} function
is called with a string argument that is either @code{"on"} or @code{"off"}
all warnings will be enabled or disabled.
It is also possible to enable and disable individual warnings through
their string identifications. The following code will issue a warning
@example
@group
warning ("non-negative-variable",
"'a' must be non-negative. Setting 'a' to zero.");
@end group
@end example
@noindent
while the following won't issue a warning
@example
@group
warning ("off", "non-negative-variable");
warning ("non-negative-variable",
"'a' must be non-negative. Setting 'a' to zero.");
@end group
@end example
The functions distributed with Octave can issue one of the following
warnings.
@c ./miscellaneous/warning_ids.m
@anchor{doc-warning_ids}
@table @code
@item Octave:array-to-scalar
If the @code{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 @code{Octave:array-to-scalar} warning is
disabled.
@item Octave:array-to-vector
If the @code{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 @code{Octave:array-to-vector} warning is
disabled.
@item Octave:assign-as-truth-value
If the @code{Octave:assign-as-truth-value} warning is
enabled, a warning is issued for statements like
@example
@group
if (s = t)
@dots{}
@end group
@end example
@noindent
since such statements are not common, and it is likely that the intent
was to write
@example
@group
if (s == t)
@dots{}
@end group
@end example
@noindent
instead.
There are times when it is useful to write code that contains
assignments within the condition of a @code{while} or @code{if}
statement. For example, statements like
@example
@group
while (c = getc())
@dots{}
@end group
@end example
@noindent
are common in C programming.
It is possible to avoid all warnings about such statements by
disabling the @code{Octave:assign-as-truth-value} warning,
but that may also let real errors like
@example
@group
if (x = 1) # intended to test (x == 1)!
@dots{}
@end group
@end example
@noindent
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
@example
@group
while ((c = getc()))
@dots{}
@end group
@end example
@noindent
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 @code{Octave:assign-as-truth-value} warning is enabled.
@item Octave:associativity-change
If the @code{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 @sc{matlab}
compatibility. By default, the @code{Octave:associativity-change}
warning is enabled.
@item Octave:divide-by-zero
If the @code{Octave:divide-by-zero} warning is enabled, a
warning is issued when Octave encounters a division by zero. By
default, the @code{Octave:divide-by-zero} warning is enabled.
@item Octave:empty-list-elements
If the @code{Octave:empty-list-elements} warning is enabled, a
warning is issued when an empty matrix is found in a matrix list.
For example,
@example
a = [1, [], 3, [], 5]
@end example
@noindent
By default, the @code{Octave:empty-list-elements} warning is enabled.
@item Octave:fortran-indexing
If the @code{Octave:fortran-indexing} warning is enabled, a warning is
printed for expressions which select elements of a two-dimensional matrix
using a single index. By default, the @code{Octave:fortran-indexing}
warning is disabled.
@item Octave:function-name-clash
If the @code{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 @code{Octave:function-name-clash} warning is enabled.
@item Octave:future-time-stamp
If the @code{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
@code{Octave:future-time-stamp} warning is enabled.
@item Octave:imag-to-real
If the @code{Octave:imag-to-real} warning is enabled, a warning is
printed for implicit conversions of complex numbers to real numbers.
By default, the @code{Octave:imag-to-real} warning is disabled.
@item Octave:matlab-incompatible
Print warnings for Octave language features that may cause
compatibility problems with @sc{matlab}.
@item Octave:missing-semicolon
If the @code{Octave:missing-semicolon} warning is enabled, Octave
will warn when statements in function definitions don't end in
semicolons. By default the @code{Octave:missing-semicolon} warning
is disabled.
@item Octave:neg-dim-as-zero
If the @code{Octave:neg-dim-as-zero} warning is enabled, print a warning
for expressions like
@example
eye (-1)
@end example
@noindent
By default, the @code{Octave:neg-dim-as-zero} warning is disabled.
@item Octave:num-to-str
If the @code{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,
@example
@group
[ "f", 111, 111 ]
@result{} "foo"
@end group
@end example
elicits a warning if the @code{Octave:num-to-str} warning is
enabled. By default, the @code{Octave:num-to-str} warning is enabled.
@item Octave:precedence-change
If the @code{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 @sc{matlab} compatibility. By default, the
@code{Octave:precedence-change} warning is enabled.
@item 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 @code{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
@code{Octave:reload-forces-clear} warning is enabled.
@item Octave:resize-on-range-error
If the @code{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
@code{Octave:resize-on-range-error} warning is disabled.
@item Octave:separator-insert
Print warning if commas or semicolons might be inserted
automatically in literal matrices.
@item Octave:single-quote-string
Print warning if a single quote character is used to introduce a
string constant.
@item Octave:str-to-num
If the @code{Octave:str-to-num} warning is enabled, a warning is printed
for implicit conversions of strings to their numeric ASCII equivalents.
For example,
@example
@group
"abc" + 0
@result{} 97 98 99
@end group
@end example
elicits a warning if the @code{Octave:str-to-num} warning is enabled.
By default, the @code{Octave:str-to-num} warning is disabled.
@item Octave:string-concat
If the @code{Octave:string-concat} warning is enabled, print a
warning when concatenating a mixture of double and single quoted strings.
By default, the @code{Octave:string-concat} warning is disabled.
@item Octave:undefined-return-values
If the @code{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
@code{Octave:undefined-return-values} warning is enabled.
@item Octave:variable-switch-label
If the @code{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 @code{Octave:variable-switch-label}
warning is disabled.
@end table
|