GNU Octave 10.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-2025 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
269 //! For given warning ID, return 0 if warnings are disabled, 1 if
270 //! enabled, and 2 if the given ID should be an error instead of a
271 //! warning.
272
273 OCTINTERP_API int warning_enabled (const std::string& id);
274
275 OCTINTERP_API void
276 verror (bool save_last_error, std::ostream& os, const char *name,
277 const char *id, const char *fmt, va_list args,
278 bool with_cfn = false);
279
280 OCTINTERP_API void
281 vwarning (const char *name, const char *id, const char *fmt,
282 va_list args);
283
284 OCTAVE_NORETURN
285 OCTINTERP_API void
286 error_1 (execution_exception& ee, const char *id, const char *fmt,
287 va_list args);
288
289 OCTAVE_NORETURN
290 OCTINTERP_API void error_1 (const char *id, const char *fmt, va_list args);
291
292 OCTAVE_NORETURN
293 OCTINTERP_API void vusage (const char *id, const char *fmt, va_list args);
294
295 OCTINTERP_API void vwarning (const char *id, const char *fmt, va_list args);
296
297 OCTAVE_NORETURN
298 OCTINTERP_API void
299 rethrow_error (const std::string& id, const std::string& msg,
300 const octave_map& stack);
301
302 OCTAVE_NORETURN
303 OCTINTERP_API void vpanic (const char *fmt, va_list args);
304
305 OCTAVE_NORETURN
306 OCTINTERP_API void panic (const char *fmt, ...);
307
308 OCTINTERP_API octave_scalar_map warning_query (const std::string& id_arg);
309
310 OCTINTERP_API std::string default_warning_state ();
311
312 OCTINTERP_API void display_warning_options (std::ostream& os);
313
314 OCTINTERP_API void
315 set_warning_option (const std::string& state, const std::string& id);
316
317 OCTINTERP_API void disable_warning (const std::string& id);
318
319 OCTINTERP_API void initialize_default_warning_state ();
320
321 OCTINTERP_API void interpreter_try (unwind_protect& frame);
322
323 // Throw execution_exception or, if debug_on_error is TRUE, enter
324 // debugger. If stack_info is empty, use current call stack.
325
326 OCTAVE_NORETURN
327 OCTINTERP_API void
328 throw_error (const std::string& err_type,
329 const std::string& id,
330 const std::string& message,
331 const std::list<frame_info>& stack_info
332 = std::list<frame_info> ());
333
334 OCTAVE_NORETURN
335 OCTINTERP_API void throw_error (execution_exception& ee);
336
337 OCTINTERP_API void save_exception (const execution_exception& ee);
338
339 OCTINTERP_API void display_exception (const execution_exception& ee) const;
340
341private:
342
343 interpreter& m_interpreter;
344
345 //! TRUE means that Octave will try to enter the debugger when an error
346 //! is encountered. This will also inhibit printing of the normal
347 //! traceback message (you will only see the top-level error message).
348
349 bool m_debug_on_error;
350
351 //! TRUE means that Octave will try to enter the debugger when an error
352 //! is encountered within the 'try' section of a 'try' / 'catch' block.
353
354 bool m_debug_on_caught;
355
356 //! TRUE means that Octave will try to enter the debugger when a warning
357 //! is encountered.
358
359 bool m_debug_on_warning;
360
361 //! TRUE means warning messages are turned off.
362
363 bool m_discard_warning_messages;
364
365 //! TRUE means that Octave will try to beep obnoxiously before
366 //! printing error messages.
367 bool m_beep_on_error;
368
369 //! TRUE means that Octave will try to display a stack trace when a
370 //! warning is encountered.
371 bool m_backtrace_on_warning;
372
373 //! TRUE means that Octave will print a verbose warning. Currently
374 //! unused.
375 bool m_verbose_warning;
376
377 //! TRUE means that Octave will print no warnings, but lastwarn will
378 //! be updated
379 bool m_quiet_warning;
380
381 //! A structure containing (most of) the current state of warnings.
382 octave_map m_warning_options;
383
384 //! The text of the last error message.
385 std::string m_last_error_message;
386
387 //! The text of the last warning message.
388 std::string m_last_warning_message;
389
390 //! The last warning message id.
391 std::string m_last_warning_id;
392
393 //! The last error message id.
394 std::string m_last_error_id;
395
396 //! The last file in which an error occurred.
397 octave_map m_last_error_stack;
398};
399
400OCTAVE_END_NAMESPACE(octave)
401
402// FIXME: should we move the following functions inside the octave
403// namespace? If so, should the functions outside of the namespace be
404// deprecated? Doing that might cause a lot of trouble... If they are
405// not deprecated and eventually removed, does it make sense to also
406// define them inside the octave namespace?
407
408extern OCTINTERP_API void
409vmessage (const char *name, const char *fmt, va_list args);
410
411OCTAVE_FORMAT_PRINTF (2, 3)
412extern OCTINTERP_API void message (const char *name, const char *fmt, ...);
413
414extern OCTINTERP_API void vwarning (const char *fmt, va_list args);
415
416OCTAVE_FORMAT_PRINTF (1, 2)
417extern OCTINTERP_API void warning (const char *fmt, ...);
418
419OCTAVE_NORETURN
420extern OCTINTERP_API void verror (const char *fmt, va_list args);
421
422OCTAVE_FORMAT_PRINTF (1, 2)
423OCTAVE_NORETURN
424extern OCTINTERP_API void error (const char *fmt, ...);
425
426OCTAVE_NORETURN
427extern OCTINTERP_API void
428verror (octave::execution_exception&, const char *fmt, va_list args);
429
430OCTAVE_FORMAT_PRINTF (2, 3)
431OCTAVE_NORETURN
432extern OCTINTERP_API void
433error (octave::execution_exception&, const char *fmt, ...);
434
435OCTAVE_NORETURN
436extern OCTINTERP_API void
437verror_with_cfn (const char *fmt, va_list args);
438
439OCTAVE_FORMAT_PRINTF (1, 2)
440OCTAVE_NORETURN
441extern OCTINTERP_API void
442error_with_cfn (const char *fmt, ...);
443
444OCTAVE_NORETURN
445extern OCTINTERP_API void
446vparse_error (const char *fmt, va_list args);
447
448OCTAVE_FORMAT_PRINTF (1, 2)
449OCTAVE_NORETURN
450extern OCTINTERP_API void
451parse_error (const char *fmt, ...);
452
453OCTAVE_NORETURN
454extern OCTINTERP_API void
455vusage_with_id (const char *id, const char *fmt, va_list args);
456
457OCTAVE_FORMAT_PRINTF (2, 3)
458OCTAVE_NORETURN
459extern OCTINTERP_API void
460usage_with_id (const char *id, const char *fmt, ...);
461
462extern OCTINTERP_API void
463vwarning_with_id (const char *id, const char *fmt, va_list args);
464
465OCTAVE_FORMAT_PRINTF (2, 3)
466extern OCTINTERP_API void
467warning_with_id (const char *id, const char *fmt, ...);
468
469OCTAVE_NORETURN
470extern OCTINTERP_API void
471verror_with_id (const char *id, const char *fmt, va_list args);
472
473OCTAVE_FORMAT_PRINTF (2, 3)
474OCTAVE_NORETURN
475extern OCTINTERP_API void
476error_with_id (const char *id, const char *fmt, ...);
477
478OCTAVE_NORETURN
479extern OCTINTERP_API void
480verror_with_id_cfn (const char *id, const char *fmt, va_list args);
481
482OCTAVE_FORMAT_PRINTF (2, 3)
483OCTAVE_NORETURN
484extern OCTINTERP_API void
485error_with_id_cfn (const char *id, const char *fmt, ...);
486
487OCTAVE_NORETURN
488extern OCTINTERP_API void
489vparse_error_with_id (const char *id, const char *fmt, va_list args);
490
491OCTAVE_FORMAT_PRINTF (2, 3)
492OCTAVE_NORETURN
493extern OCTINTERP_API void
494parse_error_with_id (const char *id, const char *fmt, ...);
495
496// Use of the following macros (error_impossible, error_if, and
497// error_unless) is discouraged. All of these will only display a
498// generic error of the form
499//
500// impossible state reached in file 'FILE' at line 'N'
501//
502// If the state really is "impossible" to reach, then it is better to
503// use one of the corresponding panic* functions instead (see panic.h).
504//
505// See also the discussion here: https://octave.discourse.group/t/assert-panic-error-and-ndebug/5409
506
507#define error_impossible() \
508 ::error ("impossible state reached in file '%s' at line %d", __FILE__, __LINE__)
509
510#define error_if(cond) do { if (cond) error_impossible (); } while (0)
511
512#define error_unless(cond) error_if (! (cond))
513
515
516//! Helper function for print_usage defined in defun.cc.
517
518extern OCTINTERP_API void defun_usage_message (const std::string& msg);
519
520// Convenience functions.
521
522extern OCTINTERP_API octave_value_list
523set_warning_state (const std::string& id, const std::string& state);
524
525extern OCTINTERP_API octave_value_list
527
528extern OCTINTERP_API int warning_enabled (const std::string& id);
529
530extern OCTINTERP_API void disable_warning (const std::string& id);
531
532extern OCTINTERP_API void interpreter_try (octave::unwind_protect&);
533
534OCTAVE_END_NAMESPACE(octave)
535
536#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:883
void display_warning_options(std::ostream &os)
Definition error.cc:750
void set_last_warning_id(const std::string &val)
Definition error.h:223
void set_discard_warning_messages(bool flag)
Definition error.h:104
static std::list< frame_info > make_stack_frame_list(const octave_map &stack)
Definition error.cc:399
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:584
OCTAVE_NORETURN void vpanic(const char *fmt, va_list args)
Definition error.cc:646
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:791
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:616
bool verbose_warning() const
Definition error.h:154
octave_scalar_map warning_query(const std::string &id_arg)
Definition error.cc:669
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:896
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:424
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:372
OCTAVE_NORETURN void vusage(const char *id, const char *fmt, va_list args)
Definition error.cc:497
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:856
void initialize_default_warning_state()
Definition error.cc:862
void vwarning(const char *name, const char *id, const char *fmt, va_list args)
Definition error.cc:506
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:927
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:726
void set_debug_on_caught(bool flag)
Definition error.h:76
void display_exception(const execution_exception &ee) const
Definition error.cc:939
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:660
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:1078
void warning_with_id(const char *id, const char *fmt,...)
Definition error.cc:1093
OCTAVE_NORETURN void vusage_with_id(const char *id, const char *fmt, va_list args)
Definition error.cc:980
void interpreter_try(octave::unwind_protect &)
OCTAVE_NORETURN void verror_with_cfn(const char *fmt, va_list args)
Definition error.cc:1027
void vwarning(const char *fmt, va_list args)
Definition error.cc:1072
void vwarning_with_id(const char *id, const char *fmt, va_list args)
Definition error.cc:1087
OCTAVE_NORETURN void usage_with_id(const char *id, const char *fmt,...)
Definition error.cc:988
OCTAVE_NORETURN void error_with_id_cfn(const char *id, const char *fmt,...)
Definition error.cc:1063
OCTAVE_NORETURN void verror_with_id(const char *id, const char *fmt, va_list args)
Definition error.cc:1042
OCTAVE_NORETURN void error(const char *fmt,...)
Definition error.cc:1003
OCTAVE_NORETURN void error_with_id(const char *id, const char *fmt,...)
Definition error.cc:1048
OCTAVE_NORETURN void verror(const char *fmt, va_list args)
Definition error.cc:997
OCTAVE_NORETURN void error_with_cfn(const char *fmt,...)
Definition error.cc:1033
OCTAVE_NORETURN void verror_with_id_cfn(const char *id, const char *fmt, va_list args)
Definition error.cc:1057
void defun_usage_message(const std::string &msg)
Helper function for print_usage defined in defun.cc.
Definition error.cc:1134
OCTAVE_NORETURN void parse_error(const char *fmt,...)
Definition error.cc:1108
void message(const char *name, const char *fmt,...)
Definition error.cc:971
OCTAVE_NORETURN void vparse_error(const char *fmt, va_list args)
Definition error.cc:1102
OCTAVE_NORETURN void vparse_error_with_id(const char *id, const char *fmt, va_list args)
Definition error.cc:1117
octave_value_list set_warning_state(const std::string &id, const std::string &state)
Definition error.cc:1835
void vmessage(const char *name, const char *fmt, va_list args)
Definition error.cc:957
void disable_warning(const std::string &id)
Definition error.cc:1864
OCTAVE_NORETURN void parse_error_with_id(const char *id, const char *fmt,...)
Definition error.cc:1123
int warning_enabled(const std::string &id)
Definition error.cc:1856