annotate doc/interpreter/errors.texi @ 3238:041ea33fbbf4

[project @ 1999-03-26 17:48:16 by jwe]
author jwe
date Fri, 26 Mar 1999 17:48:35 +0000
parents a494f93e60ff
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2679
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
1 @c Copyright (C) 1996, 1997 John W. Eaton
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
2 @c This is part of the Octave manual.
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
3 @c For copying conditions, see the file gpl.texi.
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
4
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
5 @node Error Handling, Input and Output, Functions and Scripts, Top
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
6 @chapter Error Handling
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
7
2689
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
8 Octave includes several functions for printing error and warning
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
9 messages. When you write functions that need to take special action
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
10 when they encounter abnormal conditions, you should print the error
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
11 messages using the functions described in this chapter.
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
12
2679
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
13 @deftypefn {Built-in Function} {} error (@var{template}, @dots{})
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
14 The @code{error} function formats the optional arguments under the
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
15 control of the template string @var{template} using the same rules as
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
16 the @code{printf} family of functions (@pxref{Formatted Output}).
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
17 The resulting message is prefixed by the string @samp{error: } and
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
18 printed on the @code{stderr} stream.
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
19
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
20 Calling @code{error} also sets Octave's internal error state such that
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
21 control will return to the top level without evaluating any more
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
22 commands. This is useful for aborting from functions or scripts.
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
23
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
24 If the error message does not end with a new line character, Octave will
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
25 print a traceback of all the function calls leading to the error. For
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
26 example, given the following function definitions:
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
27
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
28 @example
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
29 @group
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
30 function f () g () end
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
31 function g () h () end
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
32 function h () nargin == 1 || error ("nargin != 1"); end
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
33 @end group
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
34 @end example
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
35
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
36 @noindent
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
37 calling the function @code{f} will result in a list of messages that
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
38 can help you to quickly locate the exact location of the error:
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
39
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
40 @example
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
41 @group
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
42 f ()
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
43 error: nargin != 1
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
44 error: evaluating index expression near line 1, column 30
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
45 error: evaluating binary operator `||' near line 1, column 27
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
46 error: called from `h'
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
47 error: called from `g'
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
48 error: called from `f'
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
49 @end group
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
50 @end example
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
51
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
52 If the error message ends in a new line character, Octave will print the
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
53 message but will not display any traceback messages as it returns
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
54 control to the top level. For example, modifying the error message
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
55 in the previous example to end in a new line causes Octave to only print
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
56 a single message:
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
57
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
58 @example
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
59 @group
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
60 function h () nargin == 1 || error ("nargin != 1\n"); end
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
61 f ()
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
62 error: nargin != 1
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
63 @end group
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
64 @end example
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
65 @end deftypefn
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
66
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
67 @defvr {Built-in Variable} error_text
3156
a494f93e60ff [project @ 1998-02-20 07:47:48 by jwe]
jwe
parents: 2689
diff changeset
68 This variable contains the text of error messages that would have
2679
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
69 been printed in the body of the most recent @code{unwind_protect} or
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
70 @code{try} statement or the @var{try} part of the most recent call to
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
71 the @code{eval} function. Outside of the @code{unwind_protect} and
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
72 @code{try} statements or the @code{eval} function, or if no error has
2689
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
73 occurred within them, the value of @code{error_text} is guaranteed to be
2679
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
74 the empty string.
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
75
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
76 Note that the message does not include the first @samp{error: } prefix,
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
77 so that it may easily be passed to the @code{error} function without
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
78 additional processing@footnote{Yes, it's a kluge, but it seems to be a
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
79 reasonably useful one.}.
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
80
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
81 @xref{The try Statement} and @ref{The unwind_protect Statement}.
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
82 @end defvr
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
83
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
84 @defvr {Built-in Variable} beep_on_error
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
85 If the value of @code{beep_on_error} is nonzero, Octave will try
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
86 to ring your terminal's bell before printing an error message. The
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
87 default value is 0.
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
88 @end defvr
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
89
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
90 @deftypefn {Built-in Function} {} warning (@var{msg})
2689
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
91 Print a warning message @var{msg} prefixed by the string @samp{warning: }.
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
92 After printing the warning message, Octave will continue to execute
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
93 commands. You should use this function should when you want to notify
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
94 the user of an unusual condition, but only when it makes sense for your
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
95 program to go on.
2679
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
96 @end deftypefn
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
97
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
98 @deftypefn {Built-in Function} {} usage (@var{msg})
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
99 Print the message @var{msg}, prefixed by the string @samp{usage: }, and
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
100 set Octave's internal error state such that control will return to the
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
101 top level without evaluating any more commands. This is useful for
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
102 aborting from functions.
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
103
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
104 After @code{usage} is evaluated, Octave will print a traceback of all
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
105 the function calls leading to the usage message.
2689
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
106
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
107 You should use this function for reporting problems errors that result
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
108 from an improper call to a function, such as calling a function with an
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
109 incorrect number of arguments, or with arguments of the wrong type. For
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
110 example, most functions distributed with Octave begin with code like
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
111 this
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
112
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
113 @example
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
114 @group
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
115 if (nargin != 2)
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
116 usage ("foo (a, b)");
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
117 endif
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
118 @end group
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
119 @end example
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
120
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
121 @noindent
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2679
diff changeset
122 to check for the proper number of arguments.
2679
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
123 @end deftypefn
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
124
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
125 The following pair of functions are of limited usefulness, and may be
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
126 removed from future versions of Octave.
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
127
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
128 @deftypefn {Function File} {} perror (@var{name}, @var{num})
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
129 Print the error message for function @var{name} corresponding to the
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
130 error number @var{num}. This function is intended to be used to print
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
131 useful error messages for those functions that return numeric error
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
132 codes.
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
133 @end deftypefn
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
134
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
135 @deftypefn {Function File} {} strerror (@var{name}, @var{num})
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
136 Return the text of an error message for function @var{name}
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
137 corresponding to the error number @var{num}. This function is intended
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
138 to be used to print useful error messages for those functions that
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
139 return numeric error codes.
79c851e2f0ee [project @ 1997-02-14 04:32:00 by jwe]
jwe
parents:
diff changeset
140 @end deftypefn