1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
#pragma once
#include "Str.h"
#include "StrBuf.h"
#include "StackTrace.h"
namespace storm {
STORM_PKG(core);
template <class T>
class Array;
/**
* Base class for all exceptions in Storm.
*/
class EXCEPTION_EXPORT Exception : public Object {
STORM_EXCEPTION_BASE;
public:
// Create.
STORM_CTOR Exception();
// Copy.
Exception(const Exception &o);
// Deep copy.
virtual void STORM_FN deepCopy(CloneEnv *env);
// Retrieve the message in the exception. This function simply calls `message(StrBuf)` to generate the message.
Str *STORM_FN message() const;
// Create the message by appending it to the string buffer.
virtual void STORM_FN message(StrBuf *to) const ABSTRACT;
// Collected stack trace, if any.
StackTrace stackTrace;
// Convenience function to call to collect a stack trace. Intended to be called from the
// constructor of derived exceptions who wish to store a stack trace. This is not done by
// default, as it is fairly expensive, and not beneficial for all exceptions (e.g. syntax
// errors). Some categories of exceptions do, however, capture stack traces automatically.
// Returns `this` to allow chaining it after creation.
Exception *STORM_FN saveTrace();
// For providing a context in exception handlers.
Exception *saveTrace(void *context);
// Regular to string implementation.
virtual void STORM_FN toS(StrBuf *to) const;
};
/**
* Multiple exceptions thrown as one.
*/
class EXCEPTION_EXPORT MultiException : public Exception {
STORM_EXCEPTION;
public:
// Create.
STORM_CTOR MultiException();
// Create, with array of exceptions prepared.
STORM_CTOR MultiException(Array<Exception *> *exceptions);
// Array of exceptions.
Array<Exception *> *exceptions;
// Add an exception. Equivalent to pushing to `exceptions` directly.
void STORM_FN push(Exception *exception);
// Create a message.
virtual void STORM_FN message(StrBuf *to) const;
};
/**
* Runtime errors from various parts of the system.
*/
class EXCEPTION_EXPORT RuntimeError : public Exception {
STORM_EXCEPTION;
public:
STORM_CTOR RuntimeError();
};
/**
* Exception thrown by the garbage collector.
*
* The most common type of error is an out of memory error, but others may occur.
*/
class EXCEPTION_EXPORT GcError : public RuntimeError {
STORM_EXCEPTION;
public:
// Create, basic C-string to avoid memory allocations.
GcError(const wchar *msg);
virtual void STORM_FN message(StrBuf *to) const;
private:
// The message.
const wchar *msg;
};
/**
* Arithmetic errors. Thrown by numerical operations when an error occurs.
*/
class EXCEPTION_EXPORT NumericError : public RuntimeError {
STORM_EXCEPTION;
public:
// Create.
STORM_CTOR NumericError();
};
/**
* Division by zero. Thrown on integer divisions by zero. Floating point numbers generate
* infinity/nan in many cases instead.
*/
class EXCEPTION_EXPORT DivisionByZero : public NumericError {
STORM_EXCEPTION;
public:
// Create.
STORM_CTOR DivisionByZero();
DivisionByZero(void *context);
// Message.
virtual void STORM_FN message(StrBuf *to) const;
};
/**
* Access violation. Thrown when an invalid address has been accessed.
*/
class EXCEPTION_EXPORT MemoryAccessError : public RuntimeError {
STORM_EXCEPTION;
public:
enum Type {
notMapped,
invalidAccess,
invalidAlignment,
kernel,
};
// Create. If 'mapped' is true, then the address is mapped, but the access type was invalid.
STORM_CTOR MemoryAccessError(Word address, Type type);
MemoryAccessError(Word address, Type type, void *context);
// Message.
virtual void STORM_FN message(StrBuf *to) const;
private:
// Faulting address.
Word address;
// Type of fault.
Type type;
};
/**
* Generic exceptions.
*/
class EXCEPTION_EXPORT NotSupported : public Exception {
STORM_EXCEPTION;
public:
NotSupported(const wchar *msg);
STORM_CTOR NotSupported(Str *msg);
virtual void STORM_FN message(StrBuf *to) const;
private:
Str *msg;
};
/**
* Invalid usage.
*/
class EXCEPTION_EXPORT UsageError : public Exception {
STORM_EXCEPTION;
public:
UsageError(const wchar *msg);
STORM_CTOR UsageError(Str *msg);
virtual void STORM_FN message(StrBuf *to) const;
private:
Str *msg;
};
/**
* Internal error.
*/
class EXCEPTION_EXPORT InternalError : public RuntimeError {
STORM_EXCEPTION;
public:
InternalError(const wchar *msg);
STORM_CTOR InternalError(Str *msg);
virtual void STORM_FN message(StrBuf *to) const;
private:
Str *msg;
};
/**
* Calling an abstract function.
*/
class EXCEPTION_EXPORT AbstractFnCalled : public RuntimeError {
STORM_EXCEPTION;
public:
AbstractFnCalled(const wchar *name);
STORM_CTOR AbstractFnCalled(Str *msg);
virtual void STORM_FN message(StrBuf *to) const;
private:
Str *name;
};
/**
* Custom exception for strings. Cannot be in Str.h due to include cycles.
*/
class EXCEPTION_EXPORT StrError : public Exception {
STORM_EXCEPTION;
public:
StrError(const wchar *msg);
STORM_CTOR StrError(Str *msg);
virtual void STORM_FN message(StrBuf *to) const;
private:
Str *msg;
};
/**
* Exception thrown from the map.
*/
class EXCEPTION_EXPORT MapError : public Exception {
STORM_EXCEPTION;
public:
MapError(const wchar *msg);
STORM_CTOR MapError(Str *msg);
virtual void STORM_FN message(StrBuf *to) const;
private:
Str *msg;
};
/**
* Exception thrown from the set.
*/
class EXCEPTION_EXPORT SetError : public Exception {
STORM_EXCEPTION;
public:
SetError(const wchar *msg);
STORM_CTOR SetError(Str *msg);
virtual void STORM_FN message(StrBuf *to) const;
private:
Str *msg;
};
/**
* Custom error type for arrays.
*/
class EXCEPTION_EXPORT ArrayError : public Exception {
STORM_EXCEPTION;
public:
STORM_CTOR ArrayError(Nat id, Nat count);
STORM_CTOR ArrayError(Nat id, Nat count, Str *msg);
virtual void STORM_FN message(StrBuf *to) const;
private:
Nat id;
Nat count;
MAYBE(Str *) msg;
};
}
|