127 buffer_error_messages = 0;
136 initw.
setfield (
"identifier",
"all");
139 warning_options = initw;
151 vwarning (
const char *
name,
const char *
id,
const char *fmt, va_list args)
158 std::ostringstream output_buf;
161 output_buf << name <<
": ";
165 output_buf << std::endl;
171 std::string msg_string = output_buf.str ();
185 std::cerr << msg_string;
190 verror (
bool save_last_error, std::ostream& os,
191 const char *
name,
const char *
id,
const char *fmt, va_list args,
192 bool with_cfn =
false)
197 if (! buffer_error_messages)
204 std::ostringstream output_buf;
208 std::string base_msg = output_buf.str ();
212 std::string msg_string;
214 if (to_beep_or_not_to_beep_p)
218 msg_string += std::string (name) +
": ";
229 std::string cfn = curfcn->
name ();
233 if (cfn.length () > base_msg.length ()
234 || base_msg.compare (0, cfn.length (), cfn) != 0)
236 msg_string += cfn +
' ';
242 msg_string += base_msg +
"\n";
244 if (! error_state && save_last_error)
263 if (! buffer_error_messages)
276 const char *fmt, va_list args,
bool with_cfn =
false)
278 if (error_state != -2)
284 size_t len = strlen (fmt);
288 if (fmt[len - 1] ==
'\n')
293 tmp_fmt[len - 1] =
'\0';
294 verror (
true, os, name,
id, tmp_fmt, args, with_cfn);
302 verror (
true, os, name,
id, fmt, args, with_cfn);
311 panic (
"error_1: invalid format");
318 verror (
false, std::cerr, name,
"", fmt, args);
325 va_start (args, fmt);
334 verror (
false, std::cerr, name,
id, fmt, args);
341 va_start (args, fmt);
347 usage_1 (
const char *
id,
const char *fmt, va_list args)
349 verror (
true, std::cerr,
"usage",
id, fmt, args);
363 va_start (args, fmt);
378 va_start (args, fmt);
390 size_t len = strlen (fmt);
394 if (fmt[len - 1] ==
'\n')
399 tmp_fmt[len - 1] =
'\0';
400 verror (
false, std::cerr, 0,
"", tmp_fmt, args);
405 verror (
false, std::cerr, 0,
"", fmt, args);
410 panic (
"pr_where_2: invalid format");
417 va_start (args, fmt);
431 if (nframes_to_display > 0)
447 pr_where_1 (
" %s at line %d column %d\n", nm.c_str (),
454 error_2 (
const char *
id,
const char *fmt, va_list args,
bool with_cfn =
false)
458 error_1 (std::cerr,
"error",
id, fmt, args, with_cfn);
486 va_start (args, fmt);
501 va_start (args, fmt);
516 va_start (args, fmt);
531 va_start (args, fmt);
546 else if (state ==
"on")
548 else if (state ==
"error")
570 Cell identifier = warning_options.
contents (
"identifier");
573 bool all_found =
false;
574 bool id_found =
false;
581 if (! all_found && ovs ==
"all")
589 if (! id_found && ovs ==
id)
597 if (all_found && id_found)
611 else if (all_state == 1)
613 if (id_state == 0 || id_state == 2)
618 else if (all_state == 2)
630 warning_1 (
const char *
id,
const char *fmt, va_list args)
640 else if (warn_opt == 1)
642 vwarning (
"warning",
id, fmt, args);
675 va_start (args, fmt);
690 va_start (args, fmt);
698 error_1 (std::cerr, 0,
"", fmt, args);
705 va_start (args, fmt);
713 error_1 (std::cerr, 0,
id, fmt, args);
720 va_start (args, fmt);
729 va_start (args, fmt);
730 error_1 (std::cerr, 0,
id, fmt, args);
738 va_start (args, fmt);
739 buffer_error_messages = 0;
741 verror (
false, std::cerr,
"panic",
"", fmt, args);
750 va_start (args, fmt);
773 int nargin = args.
length ();
804 size_t len = strlen (msg);
808 if (msg[len - 1] ==
'\n')
813 tmp_msg[len - 1] =
'\0';
814 f (
id,
"%s\n", tmp_msg);
829 DEFUN (rethrow, args, ,
831 @deftypefn {Built-in Function} {} rethrow (@var{err})\n\
832 Reissue a previous error as defined by @var{err}. @var{err} is a structure\n\
833 that must contain at least the @qcode{\"message\"} and @qcode{\"identifier\"}\n\
834 fields. @var{err} can also contain a field @qcode{\"stack\"} that gives\n\
835 information on the assumed location of the error. Typically @var{err} is\n\
836 returned from @code{lasterror}.\n\
837 @seealso{lasterror, lasterr, error}\n\
841 int nargin = args.
length ();
855 int len = msg.length ();
868 if (err_stack.
numel () > 0)
885 char *tmp_msg =
strsave (msg.c_str ());
886 if (tmp_msg[len-1] ==
'\n')
890 tmp_msg[len - 1] =
'\0';
901 Vlast_error_stack = err_stack;
923 pr_where_1 (
"error: called from '%s' near line %d, column %d",
926 pr_where_1 (
"error: called from '%d' near line %d",
938 pr_where_1 (
"error: in file %s near line %d, column %d",
939 file.c_str (), l, c);
950 pr_where_1 (
"error: called from '%s' in file %s near line %d, column %d",
951 nm.c_str (), file.c_str (), l, c);
953 pr_where_1 (
"error: called from '%d' in file %s near line %d",
954 nm.c_str (), file.c_str (), l);
961 error (
"rethrow: ERR structure must contain the fields 'message and 'identifier'");
979 int nargin = args.
length ();
981 bool have_fmt = nargin > 1;
985 std::string arg1 = args(0).string_value ();
994 if (arg1.find_first_of (
"% \f\n\r\t\v") == std::string::npos
995 && arg1.find (
':') != std::string::npos
997 && arg1[arg1.length ()-1] !=
':')
1005 for (
int i = 1; i < nargin; i++)
1006 nargs(i-1) = args(i);
1009 nargs(0) =
"call to " + caller
1010 +
" with message identifier requires message";
1020 @deftypefn {Built-in Function} {} error (@var{template}, @dots{})\n\
1021 @deftypefnx {Built-in Function} {} error (@var{id}, @var{template}, @dots{})\n\
1022 Format the optional arguments under the control of the template string\n\
1023 @var{template} using the same rules as the @code{printf} family of\n\
1024 functions (@pxref{Formatted Output}) and print the resulting message\n\
1025 on the @code{stderr} stream. The message is prefixed by the character\n\
1026 string @samp{error: }.\n\
1028 Calling @code{error} also sets Octave's internal error state such that\n\
1029 control will return to the top level without evaluating any more\n\
1030 commands. This is useful for aborting from functions or scripts.\n\
1032 If the error message does not end with a new line character, Octave will\n\
1033 print a traceback of all the function calls leading to the error. For\n\
1034 example, given the following function definitions:\n\
1038 function f () g (); end\n\
1039 function g () h (); end\n\
1040 function h () nargin == 1 || error (\"nargin != 1\"); end\n\
1045 calling the function @code{f} will result in a list of messages that\n\
1046 can help you to quickly locate the exact location of the error:\n\
1051 error: nargin != 1\n\
1052 error: called from:\n\
1053 error: error at line -1, column -1\n\
1054 error: h at line 1, column 27\n\
1055 error: g at line 1, column 15\n\
1056 error: f at line 1, column 15\n\
1060 If the error message ends in a new line character, Octave will print the\n\
1061 message but will not display any traceback messages as it returns\n\
1062 control to the top level. For example, modifying the error message\n\
1063 in the previous example to end in a new line causes Octave to only print\n\
1064 a single message:\n\
1068 function h () nargin == 1 || error (\"nargin != 1\\n\"); end\n\
1070 error: nargin != 1\n\
1074 A null string (\"\") input to @code{error} will be ignored and the code\n\
1075 will continue running as if the statement were a NOP@. This is for\n\
1076 compatibility with @sc{matlab}. It also makes it possible to write code such\n\
1083 err_msg = \"CONDITION 1 found\";\n\
1084 elseif (CONDITION2)\n\
1085 err_msg = \"CONDITION 2 found\";\n\
1093 which will only stop execution if an error has been found.\n\
1095 Implementation Note: For compatibility with @sc{matlab}, escape\n\
1096 sequences (e.g., @qcode{\"\\n\"} => newline) are processed in @var{template}\n\
1097 regardless of whether @var{template} has been defined within single quotes\n\
1098 as long as there are two or more input arguments.\n\
1099 Use a second backslash to stop interpolation of the escape sequence (e.g.,\n\
1100 \"\\\\n\") or use the @code{regexptranslate} function.\n\
1101 @seealso{warning, lasterror}\n\
1106 int nargin = args.
length ();
1116 bool have_fmt =
false;
1118 if (nargin == 1 && args(0).
is_map ())
1172 std::string
id = id_arg;
1190 val =
state(i).string_value ();
1202 val =
state(i).string_value ();
1211 retval.
assign (
"identifier",
id);
1212 retval.
assign (
"state", val);
1215 error (
"warning: unable to find default warning state!");
1222 @deftypefn {Built-in Function} {} warning (@var{template}, @dots{})\n\
1223 @deftypefnx {Built-in Function} {} warning (@var{id}, @var{template}, @dots{})\n\
1224 @deftypefnx {Built-in Function} {} warning (\"on\", @var{id})\n\
1225 @deftypefnx {Built-in Function} {} warning (\"off\", @var{id})\n\
1226 @deftypefnx {Built-in Function} {} warning (\"query\", @var{id})\n\
1227 @deftypefnx {Built-in Function} {} warning (\"error\", @var{id})\n\
1228 @deftypefnx {Built-in Function} {} warning (@var{state}, @var{id}, \"local\")\n\
1229 Format the optional arguments under the control of the template string\n\
1230 @var{template} using the same rules as the @code{printf} family of\n\
1231 functions (@pxref{Formatted Output}) and print the resulting message\n\
1232 on the @code{stderr} stream. The message is prefixed by the character\n\
1233 string @samp{warning: }.\n\
1234 You should use this function when you want to notify the user\n\
1235 of an unusual condition, but only when it makes sense for your program\n\
1238 The optional message identifier allows users to enable or disable\n\
1239 warnings tagged by @var{id}. A message identifier is of the form\n\
1240 \"NAMESPACE:WARNING-NAME\". Octave's own warnings use the @qcode{\"Octave\"}\n\
1241 namespace (@pxref{XREFwarning_ids}). The special identifier @qcode{\"all\"}\n\
1242 may be used to set the state of all warnings.\n\
1244 If the first argument is @qcode{\"on\"} or @qcode{\"off\"},\n\
1245 set the state of a particular warning using the identifier @var{id}. If the\n\
1246 first argument is @qcode{\"query\"}, query the state of this warning\n\
1247 instead. If the identifier is omitted, a value of @qcode{\"all\"} is\n\
1248 assumed. If you set the state of a warning to @qcode{\"error\"}, the\n\
1249 warning named by @var{id} is handled as if it were an error instead. So,\n\
1250 for example, the following handles all warnings as errors:\n\
1254 warning (\"error\");\n\
1258 If the state is @qcode{\"on\"}, @qcode{\"off\"}, or @qcode{\"error\"}\n\
1259 and the third argument is @qcode{\"local\"}, then the warning state\n\
1260 will be set temporarily, until the end of the current function.\n\
1261 Changes to warning states that are set locally affect the current\n\
1262 function and all functions called from the current scope. The\n\
1263 previous warning state is restored on return from the current\n\
1264 function. The @qcode{\"local\"} option is ignored if used in the top-level\n\
1267 Implementation Note: For compatibility with @sc{matlab}, escape\n\
1268 sequences (e.g., @qcode{\"\\n\"} => newline) are processed in @var{template}\n\
1269 regardless of whether @var{template} has been defined within single quotes\n\
1270 as long as there are two or more input arguments.\n\
1271 Use a second backslash to stop interpolation of the escape sequence (e.g.,\n\
1272 \"\\\\n\") or use the @code{regexptranslate} function.\n\
1273 @seealso{warning_ids, lastwarn, error}\n\
1278 int nargin = args.
length ();
1279 int argc = nargin + 1;
1283 if (argc > 1 && args.all_strings_p ())
1289 std::string arg1 = argv(1);
1290 std::string arg2 =
"all";
1295 if (arg1 ==
"on" || arg1 ==
"off" || arg1 ==
"error")
1299 if (argc == 4 && argv(3) ==
"local"
1326 fields(0) =
"identifier";
1327 fields(1) =
"state";
1341 for (i = 0; i < nel; i++)
1343 std::string
id = ids(i).string_value ();
1350 states(i) = curr_state;
1364 states(nel) = curr_state;
1371 (
".saved_warning_states.", m, scope, context);
1394 && warning_options.
contains (
"identifier"))
1409 if (key ==
"Octave:matlab-incompatible"
1410 || key ==
"Octave:single-quote-string")
1424 tmp.
assign (
"identifier",
id);
1425 tmp.
assign (
"state", st);
1427 warning_options = tmp;
1431 else if (arg2 ==
"backtrace")
1433 if (arg1 !=
"error")
1439 else if (arg2 ==
"debug")
1441 if (arg1 !=
"error")
1447 else if (arg2 ==
"verbose")
1449 if (arg1 !=
"error")
1455 else if (arg2 ==
"quiet")
1457 if (arg1 !=
"error")
1488 warning_options.
assign (
"state", state);
1505 warning_options.
clear ();
1507 warning_options.
assign (
"identifier", ident);
1508 warning_options.
assign (
"state", state);
1515 if (done && nargout > 0)
1516 retval = old_warning_options;
1518 else if (arg1 ==
"query")
1522 else if (arg2 ==
"backtrace" || arg2 ==
"debug"
1523 || arg2 ==
"verbose" || arg2 ==
"quiet")
1526 tmp.
assign (
"identifier", arg2);
1527 if (arg2 ==
"backtrace")
1529 else if (arg2 ==
"debug")
1531 else if (arg2 ==
"verbose")
1562 warning_options = m;
1564 error (
"warning: expecting structure with fields 'identifier' and 'state'");
1569 retval = old_warning_options;
1573 if (! (error_state || done))
1587 "unspecified warning", nargs,
1644 DEFUN (lasterror, args, ,
1646 @deftypefn {Built-in Function} {@var{lasterr} =} lasterror ()\n\
1647 @deftypefnx {Built-in Function} {} lasterror (@var{err})\n\
1648 @deftypefnx {Built-in Function} {} lasterror (\"reset\")\n\
1649 Query or set the last error message structure. When called without\n\
1650 arguments, return a structure containing the last error message and other\n\
1651 information related to this error. The elements of the structure are:\n\
1655 The text of the last error message\n\
1658 The message identifier of this error message\n\
1661 A structure containing information on where the message occurred. This may\n\
1662 be an empty structure if the information cannot\n\
1663 be obtained. The fields of the structure are:\n\
1667 The name of the file where the error occurred\n\
1670 The name of function in which the error occurred\n\
1673 The line number at which the error occurred\n\
1676 An optional field with the column number at which the error occurred\n\
1680 The last error structure may be set by passing a scalar structure, @var{err},\n\
1681 as input. Any fields of @var{err} that match those above are set while any\n\
1682 unspecified fields are initialized with default values.\n\
1684 If @code{lasterror} is called with the argument @qcode{\"reset\"}, all\n\
1685 fields are set to their default values.\n\
1686 @seealso{lasterr, error, lastwarn}\n\
1690 int nargin = args.
length ();
1718 error (
"lasterror: unrecognized string argument");
1720 else if (args(0).
is_map ())
1724 std::string new_error_message;
1725 std::string new_error_id;
1726 std::string new_error_file;
1727 std::string new_error_name;
1728 int new_error_line = -1;
1729 int new_error_column = -1;
1731 if (! error_state && new_err.
contains (
"message"))
1733 const std::string tmp =
1735 new_error_message = tmp;
1738 if (! error_state && new_err.
contains (
"identifier"))
1740 const std::string tmp =
1745 if (! error_state && new_err.
contains (
"stack"))
1750 if (! error_state && new_err_stack.
contains (
"file"))
1752 const std::string tmp =
1754 new_error_file = tmp;
1757 if (! error_state && new_err_stack.
contains (
"name"))
1759 const std::string tmp =
1761 new_error_name = tmp;
1764 if (! error_state && new_err_stack.
contains (
"line"))
1768 new_error_line = tmp;
1771 if (! error_state && new_err_stack.
contains (
"column"))
1775 new_error_column = tmp;
1786 new_err_stack.
setfield (
"file", new_error_file);
1787 new_err_stack.
setfield (
"name", new_error_name);
1788 new_err_stack.
setfield (
"line", new_error_line);
1789 new_err_stack.
setfield (
"column", new_error_column);
1790 Vlast_error_stack = new_err_stack;
1803 error (
"lasterror: argument must be a structure or a string");
1815 DEFUN (lasterr, args, nargout,
1817 @deftypefn {Built-in Function} {[@var{msg}, @var{msgid}] =} lasterr ()\n\
1818 @deftypefnx {Built-in Function} {} lasterr (@var{msg})\n\
1819 @deftypefnx {Built-in Function} {} lasterr (@var{msg}, @var{msgid})\n\
1820 Query or set the last error message. When called without input arguments,\n\
1821 return the last error message and message identifier. With one\n\
1822 argument, set the last error message to @var{msg}. With two arguments,\n\
1823 also set the last message identifier.\n\
1824 @seealso{lasterror, error, lastwarn}\n\
1834 int argc = args.length () + 1;
1851 if (argc == 1 || nargout > 0)
1853 retval(1) = prev_error_id;
1854 retval(0) = prev_error_message;
1858 error (
"lasterr: expecting arguments to be character strings");
1866 DEFUN (lastwarn, args, nargout,
1868 @deftypefn {Built-in Function} {[@var{msg}, @var{msgid}] =} lastwarn ()\n\
1869 @deftypefnx {Built-in Function} {} lastwarn (@var{msg})\n\
1870 @deftypefnx {Built-in Function} {} lastwarn (@var{msg}, @var{msgid})\n\
1871 Query or set the last warning message. When called without input arguments,\n\
1872 return the last warning message and message identifier. With one\n\
1873 argument, set the last warning message to @var{msg}. With two arguments,\n\
1874 also set the last message identifier.\n\
1875 @seealso{warning, lasterror, lasterr}\n\
1880 int argc = args.
length () + 1;
1897 if (argc == 1 || nargout > 0)
1900 retval(1) = prev_warning_id;
1901 retval(0) = prev_warning_message;
1905 error (
"lastwarn: expecting arguments to be character strings");
1915 @deftypefn {Built-in Function} {} usage (@var{msg})\n\
1916 Print the message @var{msg}, prefixed by the string @samp{usage: }, and\n\
1917 set Octave's internal error state such that control will return to the\n\
1918 top level without evaluating any more commands. This is useful for\n\
1919 aborting from functions.\n\
1921 After @code{usage} is evaluated, Octave will print a traceback of all\n\
1922 the function calls leading to the usage message.\n\
1924 You should use this function for reporting problems errors that result\n\
1925 from an improper call to a function, such as calling a function with an\n\
1926 incorrect number of arguments, or with arguments of the wrong type. For\n\
1927 example, most functions distributed with Octave begin with code like\n\
1933 usage (\"foo (a, b)\");\n\
1939 to check for the proper number of arguments.\n\
1947 DEFUN (beep_on_error, args, nargout,
1949 @deftypefn {Built-in Function} {@var{val} =} beep_on_error ()\n\
1950 @deftypefnx {Built-in Function} {@var{old_val} =} beep_on_error (@var{new_val})\n\
1951 @deftypefnx {Built-in Function} {} beep_on_error (@var{new_val}, \"local\")\n\
1952 Query or set the internal variable that controls whether Octave will try\n\
1953 to ring the terminal bell before printing an error message.\n\
1955 When called from inside a function with the @qcode{\"local\"} option, the\n\
1956 variable is changed locally for the function and any subroutines it calls. \n\
1957 The original variable value is restored when exiting the function.\n\
1963 DEFUN (debug_on_error, args, nargout,
1965 @deftypefn {Built-in Function} {@var{val} =} debug_on_error ()\n\
1966 @deftypefnx {Built-in Function} {@var{old_val} =} debug_on_error (@var{new_val})\n\
1967 @deftypefnx {Built-in Function} {} debug_on_error (@var{new_val}, \"local\")\n\
1968 Query or set the internal variable that controls whether Octave will try\n\
1969 to enter the debugger when an error is encountered. This will also\n\
1970 inhibit printing of the normal traceback message (you will only see\n\
1971 the top-level error message).\n\
1973 When called from inside a function with the @qcode{\"local\"} option, the\n\
1974 variable is changed locally for the function and any subroutines it calls. \n\
1975 The original variable value is restored when exiting the function.\n\
1976 @seealso{debug_on_warning, debug_on_interrupt}\n\
1982 DEFUN (debug_on_warning, args, nargout,
1984 @deftypefn {Built-in Function} {@var{val} =} debug_on_warning ()\n\
1985 @deftypefnx {Built-in Function} {@var{old_val} =} debug_on_warning (@var{new_val})\n\
1986 @deftypefnx {Built-in Function} {} debug_on_warning (@var{new_val}, \"local\")\n\
1987 Query or set the internal variable that controls whether Octave will try\n\
1988 to enter the debugger when a warning is encountered.\n\
1990 When called from inside a function with the @qcode{\"local\"} option, the\n\
1991 variable is changed locally for the function and any subroutines it calls. \n\
1992 The original variable value is restored when exiting the function.\n\
1993 @seealso{debug_on_error, debug_on_interrupt}\n\
2031 buffer_error_messages++;