29
/// Exception handler class
31
/// Exception handler has a severity (exceptionLevel), an error message and a
32
/// location. Also function to return the stack trace (gcc only).
34
/// If you aren't using gcc, you need to define NO_STACKTRACE
36
// note the throw() directives are there to explicitly declare that we cannot
37
// throw an exception in this stuff
39
class Squeal : public std::exception {
30
/** MAUS exception class
32
* Exception has a severity (exceptionLevel), an error message and a
33
* location. Also function to return the stack trace (gcc only).
35
* If you aren't using gcc, you need to define NO_STACKTRACE at compile time
37
* Note the throw() directives are there to explicitly declare that we cannot
38
* throw an exception in this stuff
40
class Exception : public std::exception {
41
/// ExceptionLevel enumerates the severity of the exception.
43
/// I use an enumeration to distinguish between different error levels.
44
/// * nonRecoverable means the internal state of the programme is no longer
45
/// well-defined i.e. some memory problem or so.
46
/// * recoverable means that in principle we could keep on running, although
47
/// most of the time this results in end of run (usually indicates typo in
49
/// If we start doing GUIs, then the distinction becomes important. Most stuff
50
/// should be recoverable.
42
/** ExceptionLevel enumerates the severity of the exception.
44
* I use an enumeration to distinguish between different error levels.
45
* - nonRecoverable means the internal state of the programme is no longer
46
* well-defined i.e. some memory problem or so.
47
* - recoverable means that in principle we could keep on running, although
48
* most of the time this results in end of run (usually indicates typo in
50
* If we start doing GUIs, then the distinction becomes important. Most stuff
51
* should be recoverable.
51
53
enum exceptionLevel {recoverable, nonRecoverable};
53
/// constructor - with error level, error message and location
55
/// note this makes a stack trace which can be slow.
56
Squeal(exceptionLevel level, std::string errorMessage, std::string location)
55
/** constructor - with error level, error message and location
57
* note this makes a stack trace which can be slow.
59
Exception(exceptionLevel level, std::string errorMessage, std::string location)
58
/// constructor - does nothing
61
/// destructor - does nothing
63
/// Return char buffer of message+" at "+location
61
/** constructor - does nothing */
64
/** destructor - does nothing */
65
~Exception() throw() {}
67
/** Return char buffer of message+" at "+location
69
* Memory remains owned by Exception
64
71
const char* what() const throw() {return &_what[0];}
65
/// Print the Message to Squeak::mout(Squeak::error) and Location to
66
/// Squeak::mout(Squeak::debug)
73
/** Print the error message and location
75
* Prints to error message to Squeak::mout(Squeak::error) and Location to
76
* Squeak::mout(Squeak::debug)
68
/// Get the severity of the exception
69
exceptionLevel GetErrorLevel() const {return _level;}
70
/// Get the error message (as defined by constructor)
71
std::string GetMessage() const {return _message;}
72
/// Set the error message
73
void SetMessage(std::string new_message) {
74
_message = new_message;
75
SetWhat(_message+" at "+_location);
77
/// Get the location (as defined by constructor) of the error
78
std::string GetLocation() const {return _location;}
79
/// Return the stack trace if it was stored
80
std::string GetStackTrace() const {return _stacktrace;}
81
/// Gcc-specific code to recover the stack trace as a string.
83
/// Will skip traces below skipTrace (so, for example, if we want to know
84
/// where the Squeal was thrown we might set skipTrace to 2, to skip
85
/// MakeStackTrace and Squeal constructor).
86
static std::string MakeStackTrace(size_t skipTrace);
80
/** Get the severity of the exception */
81
inline exceptionLevel GetErrorLevel() const {return _level;}
83
/** Get the error message (as defined by constructor) */
84
inline std::string GetMessage() const {return _message;}
86
/** Set the error message */
87
inline void SetMessage(std::string new_message);
89
/** Get the location (as defined by Exception constructor) of the error */
90
std::string GetLocation() const {return _location;}
92
/** Return the stack trace if it was stored */
93
std::string GetStackTrace() const {return _stacktrace;}
95
/** Gcc-specific code to recover the stack trace as a string.
97
* Will skip traces below skipTrace (so, for example, if we want to know
98
* where the Exception was thrown we might set skipTrace to 2, to skip
99
* MakeStackTrace and Exception constructor).
101
static std::string MakeStackTrace(size_t skipTrace);
103
/** Set that it makes a stack trace on exception */
104
static void SetWillDoStackTrace(bool willDoStackTrace);
106
/** Return whether makes a stack trace on exception */
107
static bool GetWillDoStackTrace();
90
void SetWhat(std::string what_str) {
91
_what = std::vector<char>(what_str.size()+1);
92
snprintf(&_what[0], what_str.size()+1, "%s", what_str.c_str());
95
static const size_t _maxStackSize;
110
inline void SetWhat(std::string what_str);
112
static const size_t _maxStackSize;
113
static bool _willDoStackTrace;
97
115
std::string _message;
98
116
std::string _location;