GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
error.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1993-2026 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if ! defined (octave_error_h)
27#define octave_error_h 1
28
29#include "octave-config.h"
30
31#include <cstdarg>
32#include <cinttypes>
33#include <string>
34
35#include "unwind-prot.h"
36
37#include "oct-map.h"
38// Include panic.h here for backward compatibility with previous
39// versions of Octave that declared the global panic functions and
40// macros here.
41#include "panic.h"
42
44
46
48
50{
51public:
52
53 OCTINTERP_API error_system (interpreter& interp);
54
55 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (error_system)
56
57 ~error_system () = default;
58
59 OCTINTERP_API octave_value
60 debug_on_error (const octave_value_list& args, int nargout);
61
62 void set_debug_on_error (bool flag) { m_debug_on_error = flag; }
63
64 bool debug_on_error () const { return m_debug_on_error; }
65
66 bool debug_on_error (bool flag)
67 {
68 bool val = m_debug_on_error;
69 m_debug_on_error = flag;
70 return val;
71 }
72
73 OCTINTERP_API octave_value
74 debug_on_caught (const octave_value_list& args, int nargout);
75
76 void set_debug_on_caught (bool flag) { m_debug_on_caught = flag; }
77
78 bool debug_on_caught () const { return m_debug_on_caught; }
79
80 bool debug_on_caught (bool flag)
81 {
82 bool val = m_debug_on_caught;
83 m_debug_on_caught = flag;
84 return val;
85 }
86
87 OCTINTERP_API octave_value
88 debug_on_warning (const octave_value_list& args, int nargout);
89
90 void set_debug_on_warning (bool flag) { m_debug_on_warning = flag; }
91
92 bool debug_on_warning () const { return m_debug_on_warning; }
93
94 bool debug_on_warning (bool flag)
95 {
96 bool val = m_debug_on_warning;
97 m_debug_on_warning = flag;
98 return val;
99 }
100
101 OCTINTERP_API octave_value
102 discard_warning_messages (const octave_value_list& args, int nargout);
103
105 {
106 m_discard_warning_messages = flag;
107 }
108
110 {
111 return m_discard_warning_messages;
112 }
113
115 {
116 bool val = m_discard_warning_messages;
117 m_discard_warning_messages = flag;
118 return val;
119 }
120
121 OCTINTERP_API octave_value
122 beep_on_error (const octave_value_list& args, int nargout);
123
124 void set_beep_on_error (bool flag) { m_beep_on_error = flag; }
125
126 bool beep_on_error () const { return m_beep_on_error; }
127
128 bool beep_on_error (bool flag)
129 {
130 bool val = m_beep_on_error;
131 m_beep_on_error = flag;
132 return val;
133 }
134
135 OCTINTERP_API octave_value
136 backtrace_on_warning (const octave_value_list& args, int nargout);
137
138 void set_backtrace_on_warning (bool flag) { m_backtrace_on_warning = flag; }
139
140 bool backtrace_on_warning () const { return m_backtrace_on_warning; }
141
142 bool backtrace_on_warning (bool flag)
143 {
144 bool val = m_backtrace_on_warning;
145 m_backtrace_on_warning = flag;
146 return val;
147 }
148
149 OCTINTERP_API octave_value
150 verbose_warning (const octave_value_list& args, int nargout);
151
152 void set_verbose_warning (bool flag) { m_verbose_warning = flag; }
153
154 bool verbose_warning () const { return m_verbose_warning; }
155
156 bool verbose_warning (bool flag)
157 {
158 bool val = m_verbose_warning;
159 m_verbose_warning = flag;
160 return val;
161 }
162
163 OCTINTERP_API octave_value
164 quiet_warning (const octave_value_list& args, int nargout);
165
166 void set_quiet_warning (bool flag) { m_quiet_warning = flag; }
167
168 bool quiet_warning () const { return m_quiet_warning; }
169
170 bool quiet_warning (bool flag)
171 {
172 bool val = m_quiet_warning;
173 m_quiet_warning = flag;
174 return val;
175 }
176
177 octave_map warning_options () const { return m_warning_options; }
178
180 { m_warning_options = val; }
181
183 {
184 octave_map val = m_warning_options;
185 m_warning_options = new_val;
186 return val;
187 }
188
189 OCTINTERP_API octave_value
190 last_error_message (const octave_value_list& args, int nargout);
191
192 void set_last_error_message (const std::string& val)
193 { m_last_error_message = val; }
194
195 std::string last_error_message () const { return m_last_error_message; }
196
197 std::string last_error_message (const std::string& s)
198 {
199 std::string val = m_last_error_message;
200 m_last_error_message = s;
201 return val;
202 }
203
204 OCTINTERP_API octave_value
205 last_warning_message (const octave_value_list& args, int nargout);
206
207 void set_last_warning_message (const std::string& val)
208 { m_last_warning_message = val; }
209
210 std::string last_warning_message () const
211 { return m_last_warning_message; }
212
213 std::string last_warning_message (const std::string& s)
214 {
215 std::string val = m_last_warning_message;
216 m_last_warning_message = s;
217 return val;
218 }
219
220 OCTINTERP_API octave_value
221 last_warning_id (const octave_value_list& args, int nargout);
222
223 void set_last_warning_id (const std::string& val)
224 { m_last_warning_id = val; }
225
226 std::string last_warning_id () const { return m_last_warning_id; }
227
228 std::string last_warning_id (const std::string& s)
229 {
230 std::string val = m_last_warning_id;
231 m_last_warning_id = s;
232 return val;
233 }
234
235 OCTINTERP_API octave_value
236 last_error_id (const octave_value_list& args, int nargout);
237
238 void set_last_error_id (const std::string& val) { m_last_error_id = val; }
239
240 std::string last_error_id () const { return m_last_error_id; }
241
242 std::string last_error_id (const std::string& s)
243 {
244 std::string val = m_last_error_id;
245 m_last_error_id = s;
246 return val;
247 }
248
250 {
251 m_last_error_stack = val;
252 }
253
254 octave_map last_error_stack () const { return m_last_error_stack; }
255
257 {
258 octave_map val = m_last_error_stack;
259 m_last_error_stack = new_val;
260 return val;
261 }
262
263 static OCTINTERP_API octave_map
264 make_stack_map (const std::list<frame_info>& frames);
265
266 static OCTINTERP_API std::list<frame_info>
267 make_stack_frame_list (const octave_map& stack,
268 const std::string& who = "error");
269
270 //! For given warning ID, return 0 if warnings are disabled, 1 if
271 //! enabled, and 2 if the given ID should be an error instead of a
272 //! warning.
273
274 OCTINTERP_API int warning_enabled (const std::string& id);
275
276 OCTINTERP_API void
277 verror (bool save_last_error, std::ostream& os, const char *name,
278 const char *id, const char *fmt, va_list args,
279 bool with_cfn = false);
280
281 OCTINTERP_API void
282 vwarning (const char *name, const char *id, const char *fmt,
283 va_list args);
284
285 OCTAVE_NORETURN
286 OCTINTERP_API void
287 error_1 (execution_exception& ee, const char *id, const char *fmt,
288 va_list args);
289
290 OCTAVE_NORETURN
291 OCTINTERP_API void error_1 (const char *id, const char *fmt, va_list args);
292
293 OCTAVE_NORETURN
294 OCTINTERP_API void vusage (const char *id, const char *fmt, va_list args);
295
296 OCTINTERP_API void vwarning (const char *id, const char *fmt, va_list args);
297
298 OCTAVE_NORETURN
299 OCTINTERP_API void
300 rethrow_error (const std::string& id, const std::string& msg,
301 const octave_map& stack);
302
303 OCTAVE_NORETURN
304 OCTINTERP_API void vpanic (const char *fmt, va_list args);
305
306 OCTAVE_NORETURN
307 OCTINTERP_API void panic (const char *fmt, ...);
308
309 OCTINTERP_API octave_scalar_map warning_query (const std::string& id_arg);
310
311 OCTINTERP_API std::string default_warning_state ();
312
313 OCTINTERP_API void display_warning_options (std::ostream& os);
314
315 OCTINTERP_API void
316 set_warning_option (const std::string& state, const std::string& id);
317
318 OCTINTERP_API void disable_warning (const std::string& id);
319
320 OCTINTERP_API void initialize_default_warning_state ();
321
322 OCTINTERP_API void interpreter_try (unwind_protect& frame);
323
324 // Throw execution_exception or, if debug_on_error is TRUE, enter
325 // debugger. If stack_info is empty, use current call stack.
326
327 OCTAVE_NORETURN
328 OCTINTERP_API void
329 throw_error (const std::string& err_type,
330 const std::string& id,
331 const std::string& message,
332 const std::list<frame_info>& stack_info
333 = std::list<frame_info> ());
334
335 OCTAVE_NORETURN
336 OCTINTERP_API void throw_error (execution_exception& ee);
337
338 OCTINTERP_API void save_exception (const execution_exception& ee);
339
340 OCTINTERP_API void display_exception (const execution_exception& ee) const;
341
342private:
343
344 interpreter& m_interpreter;
345
346 //! TRUE means that Octave will try to enter the debugger when an error
347 //! is encountered. This will also inhibit printing of the normal
348 //! traceback message (you will only see the top-level error message).
349
350 bool m_debug_on_error;
351
352 //! TRUE means that Octave will try to enter the debugger when an error
353 //! is encountered within the 'try' section of a 'try' / 'catch' block.
354
355 bool m_debug_on_caught;
356
357 //! TRUE means that Octave will try to enter the debugger when a warning
358 //! is encountered.
359
360 bool m_debug_on_warning;
361
362 //! TRUE means warning messages are turned off.
363
364 bool m_discard_warning_messages;
365
366 //! TRUE means that Octave will try to beep obnoxiously before
367 //! printing error messages.
368 bool m_beep_on_error;
369
370 //! TRUE means that Octave will try to display a stack trace when a
371 //! warning is encountered.
372 bool m_backtrace_on_warning;
373
374 //! TRUE means that Octave will print a verbose warning. Currently
375 //! unused.
376 bool m_verbose_warning;
377
378 //! TRUE means that Octave will print no warnings, but lastwarn will
379 //! be updated
380 bool m_quiet_warning;
381
382 //! A structure containing (most of) the current state of warnings.
383 octave_map m_warning_options;
384
385 //! The text of the last error message.
386 std::string m_last_error_message;
387
388 //! The text of the last warning message.
389 std::string m_last_warning_message;
390
391 //! The last warning message id.
392 std::string m_last_warning_id;
393
394 //! The last error message id.
395 std::string m_last_error_id;
396
397 //! The last file in which an error occurred.
398 octave_map m_last_error_stack;
399};
400
401OCTAVE_END_NAMESPACE(octave)
402
403// FIXME: should we move the following functions inside the octave
404// namespace? If so, should the functions outside of the namespace be
405// deprecated? Doing that might cause a lot of trouble... If they are
406// not deprecated and eventually removed, does it make sense to also
407// define them inside the octave namespace?
408
409extern OCTINTERP_API void
410vmessage (const char *name, const char *fmt, va_list args);
411
412OCTAVE_FORMAT_PRINTF (2, 3)
413extern OCTINTERP_API void message (const char *name, const char *fmt, ...);
414
415extern OCTINTERP_API void vwarning (const char *fmt, va_list args);
416
417OCTAVE_FORMAT_PRINTF (1, 2)
418extern OCTINTERP_API void warning (const char *fmt, ...);
419
420OCTAVE_NORETURN
421extern OCTINTERP_API void verror (const char *fmt, va_list args);
422
423OCTAVE_FORMAT_PRINTF (1, 2)
424OCTAVE_NORETURN
425extern OCTINTERP_API void error (const char *fmt, ...);
426
427OCTAVE_NORETURN
428extern OCTINTERP_API void
429verror (octave::execution_exception&, const char *fmt, va_list args);
430
431OCTAVE_FORMAT_PRINTF (2, 3)
432OCTAVE_NORETURN
433extern OCTINTERP_API void
434error (octave::execution_exception&, const char *fmt, ...);
435
436OCTAVE_NORETURN
437extern OCTINTERP_API void
438verror_with_cfn (const char *fmt, va_list args);
439
440OCTAVE_FORMAT_PRINTF (1, 2)
441OCTAVE_NORETURN
442extern OCTINTERP_API void
443error_with_cfn (const char *fmt, ...);
444
445OCTAVE_NORETURN
446extern OCTINTERP_API void
447vparse_error (const char *fmt, va_list args);
448
449OCTAVE_FORMAT_PRINTF (1, 2)
450OCTAVE_NORETURN
451extern OCTINTERP_API void
452parse_error (const char *fmt, ...);
453
454OCTAVE_NORETURN
455extern OCTINTERP_API void
456vusage_with_id (const char *id, const char *fmt, va_list args);
457
458OCTAVE_FORMAT_PRINTF (2, 3)
459OCTAVE_NORETURN
460extern OCTINTERP_API void
461usage_with_id (const char *id, const char *fmt, ...);
462
463extern OCTINTERP_API void
464vwarning_with_id (const char *id, const char *fmt, va_list args);
465
466OCTAVE_FORMAT_PRINTF (2, 3)
467extern OCTINTERP_API void
468warning_with_id (const char *id, const char *fmt, ...);
469
470OCTAVE_NORETURN
471extern OCTINTERP_API void
472verror_with_id (const char *id, const char *fmt, va_list args);
473
474OCTAVE_FORMAT_PRINTF (2, 3)
475OCTAVE_NORETURN
476extern OCTINTERP_API void
477error_with_id (const char *id, const char *fmt, ...);
478
479OCTAVE_NORETURN
480extern OCTINTERP_API void
481verror_with_id_cfn (const char *id, const char *fmt, va_list args);
482
483OCTAVE_FORMAT_PRINTF (2, 3)
484OCTAVE_NORETURN
485extern OCTINTERP_API void
486error_with_id_cfn (const char *id, const char *fmt, ...);
487
488OCTAVE_NORETURN
489extern OCTINTERP_API void
490vparse_error_with_id (const char *id, const char *fmt, va_list args);
491
492OCTAVE_FORMAT_PRINTF (2, 3)
493OCTAVE_NORETURN
494extern OCTINTERP_API void
495parse_error_with_id (const char *id, const char *fmt, ...);
496
497// Use of the following macros (error_impossible, error_if, and
498// error_unless) is discouraged. All of these will only display a
499// generic error of the form
500//
501// impossible state reached in file 'FILE' at line 'N'
502//
503// If the state really is "impossible" to reach, then it is better to
504// use one of the corresponding panic* functions instead (see panic.h).
505//
506// See also the discussion here: https://octave.discourse.group/t/assert-panic-error-and-ndebug/5409
507
508#define error_impossible() \
509 ::error ("impossible state reached in file '%s' at line %d", __FILE__, __LINE__)
510
511#define error_if(cond) do { if (cond) error_impossible (); } while (0)
512
513#define error_unless(cond) error_if (! (cond))
514
516
517//! Helper function for print_usage defined in defun.cc.
518
519extern OCTINTERP_API void defun_usage_message (const std::string& msg);
520
521// Convenience functions.
522
523extern OCTINTERP_API octave_value_list
524set_warning_state (const std::string& id, const std::string& state);
525
526extern OCTINTERP_API octave_value_list
528
529extern OCTINTERP_API int warning_enabled (const std::string& id);
530
531extern OCTINTERP_API void disable_warning (const std::string& id);
532
533extern OCTINTERP_API void interpreter_try (octave::unwind_protect&);
534
535OCTAVE_END_NAMESPACE(octave)
536
537#endif
~error_system()=default
bool debug_on_error(bool flag)
Definition error.h:66
std::string last_error_message() const
Definition error.h:195
void set_beep_on_error(bool flag)
Definition error.h:124
void interpreter_try(unwind_protect &frame)
Definition error.cc:888
void display_warning_options(std::ostream &os)
Definition error.cc:755
static std::list< frame_info > make_stack_frame_list(const octave_map &stack, const std::string &who="error")
Definition error.cc:421
void set_last_warning_id(const std::string &val)
Definition error.h:223
void set_discard_warning_messages(bool flag)
Definition error.h:104
void set_quiet_warning(bool flag)
Definition error.h:166
void set_last_warning_message(const std::string &val)
Definition error.h:207
std::string last_warning_id() const
Definition error.h:226
bool debug_on_error() const
Definition error.h:64
OCTAVE_NORETURN void error_1(execution_exception &ee, const char *id, const char *fmt, va_list args)
Definition error.cc:604
OCTAVE_NORETURN void vpanic(const char *fmt, va_list args)
Definition error.cc:651
std::string last_warning_id(const std::string &s)
Definition error.h:228
bool debug_on_warning() const
Definition error.h:92
void verror(bool save_last_error, std::ostream &os, const char *name, const char *id, const char *fmt, va_list args, bool with_cfn=false)
void set_warning_option(const std::string &state, const std::string &id)
Definition error.cc:796
std::string last_warning_message(const std::string &s)
Definition error.h:213
bool beep_on_error() const
Definition error.h:126
OCTAVE_NORETURN void rethrow_error(const std::string &id, const std::string &msg, const octave_map &stack)
Definition error.cc:636
bool verbose_warning() const
Definition error.h:154
octave_scalar_map warning_query(const std::string &id_arg)
Definition error.cc:674
std::string last_error_id(const std::string &s)
Definition error.h:242
OCTAVE_NORETURN void throw_error(const std::string &err_type, const std::string &id, const std::string &message, const std::list< frame_info > &stack_info=std::list< frame_info >())
Definition error.cc:901
octave_map last_error_stack() const
Definition error.h:254
int warning_enabled(const std::string &id)
For given warning ID, return 0 if warnings are disabled, 1 if enabled, and 2 if the given ID should b...
Definition error.cc:444
std::string last_warning_message() const
Definition error.h:210
octave_map warning_options(const octave_map &new_val)
Definition error.h:182
bool verbose_warning(bool flag)
Definition error.h:156
octave_map last_error_stack(const octave_map &new_val)
Definition error.h:256
static octave_map make_stack_map(const std::list< frame_info > &frames)
Definition error.cc:373
OCTAVE_NORETURN void vusage(const char *id, const char *fmt, va_list args)
Definition error.cc:517
bool backtrace_on_warning(bool flag)
Definition error.h:142
bool debug_on_caught(bool flag)
Definition error.h:80
bool discard_warning_messages() const
Definition error.h:109
octave_map warning_options() const
Definition error.h:177
void set_last_error_stack(const octave_map &val)
Definition error.h:249
void disable_warning(const std::string &id)
Definition error.cc:861
void initialize_default_warning_state()
Definition error.cc:867
void vwarning(const char *name, const char *id, const char *fmt, va_list args)
Definition error.cc:526
void set_debug_on_warning(bool flag)
Definition error.h:90
bool debug_on_caught() const
Definition error.h:78
void set_verbose_warning(bool flag)
Definition error.h:152
std::string last_error_id() const
Definition error.h:240
void save_exception(const execution_exception &ee)
Definition error.cc:932
void set_last_error_id(const std::string &val)
Definition error.h:238
bool discard_warning_messages(bool flag)
Definition error.h:114
void set_backtrace_on_warning(bool flag)
Definition error.h:138
bool quiet_warning() const
Definition error.h:168
bool quiet_warning(bool flag)
Definition error.h:170
bool beep_on_error(bool flag)
Definition error.h:128
bool backtrace_on_warning() const
Definition error.h:140
std::string default_warning_state()
Definition error.cc:731
void set_debug_on_caught(bool flag)
Definition error.h:76
void display_exception(const execution_exception &ee) const
Definition error.cc:944
bool debug_on_warning(bool flag)
Definition error.h:94
std::string last_error_message(const std::string &s)
Definition error.h:197
void set_warning_options(const octave_map &val)
Definition error.h:179
OCTAVE_NORETURN void panic(const char *fmt,...)
Definition error.cc:665
void set_last_error_message(const std::string &val)
Definition error.h:192
void set_debug_on_error(bool flag)
Definition error.h:62
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void warning(const char *fmt,...)
Definition error.cc:1083
void warning_with_id(const char *id, const char *fmt,...)
Definition error.cc:1098
OCTAVE_NORETURN void vusage_with_id(const char *id, const char *fmt, va_list args)
Definition error.cc:985
void interpreter_try(octave::unwind_protect &)
OCTAVE_NORETURN void verror_with_cfn(const char *fmt, va_list args)
Definition error.cc:1032
void vwarning(const char *fmt, va_list args)
Definition error.cc:1077
void vwarning_with_id(const char *id, const char *fmt, va_list args)
Definition error.cc:1092
OCTAVE_NORETURN void usage_with_id(const char *id, const char *fmt,...)
Definition error.cc:993
OCTAVE_NORETURN void error_with_id_cfn(const char *id, const char *fmt,...)
Definition error.cc:1068
OCTAVE_NORETURN void verror_with_id(const char *id, const char *fmt, va_list args)
Definition error.cc:1047
OCTAVE_NORETURN void error(const char *fmt,...)
Definition error.cc:1008
OCTAVE_NORETURN void error_with_id(const char *id, const char *fmt,...)
Definition error.cc:1053
OCTAVE_NORETURN void verror(const char *fmt, va_list args)
Definition error.cc:1002
OCTAVE_NORETURN void error_with_cfn(const char *fmt,...)
Definition error.cc:1038
OCTAVE_NORETURN void verror_with_id_cfn(const char *id, const char *fmt, va_list args)
Definition error.cc:1062
void defun_usage_message(const std::string &msg)
Helper function for print_usage defined in defun.cc.
Definition error.cc:1139
OCTAVE_NORETURN void parse_error(const char *fmt,...)
Definition error.cc:1113
void message(const char *name, const char *fmt,...)
Definition error.cc:976
OCTAVE_NORETURN void vparse_error(const char *fmt, va_list args)
Definition error.cc:1107
OCTAVE_NORETURN void vparse_error_with_id(const char *id, const char *fmt, va_list args)
Definition error.cc:1122
octave_value_list set_warning_state(const std::string &id, const std::string &state)
Definition error.cc:1948
void vmessage(const char *name, const char *fmt, va_list args)
Definition error.cc:962
void disable_warning(const std::string &id)
Definition error.cc:1977
OCTAVE_NORETURN void parse_error_with_id(const char *id, const char *fmt,...)
Definition error.cc:1128
int warning_enabled(const std::string &id)
Definition error.cc:1969