forked from rubinius/rubinius
/
exception.hpp
120 lines (91 loc) · 2.9 KB
/
exception.hpp
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
#ifndef RBX_EXCEPTION_HPP
#define RBX_EXCEPTION_HPP
#include <string>
#include <cstdlib>
#include <cstring>
#include "object_types.hpp"
#include "prelude.hpp"
#include "builtin/object.hpp"
namespace rubinius {
class Exception;
void abort();
void bug(const char* message);
void bug(const char* message, const char* arg);
void print_backtrace(size_t max=100);
/**
* Base class for the various exception.
*
* Each exception class should define a +raise+ function. Code
* should call the +raise+ function instead of throwing one of
* the exceptions directly. This allows setting a breakpoint
* on +raise+ to more easily debug exceptions. For example, to
* cause a Assertion exception, use
*
* Assertion::raise(type, obj, reason);
*
* instead of
*
* throw Assertion(type, obj, reason);
*/
class VMException {
public: /* Instance vars */
typedef std::vector<std::string> Backtrace;
Backtrace* backtrace;
char* reason;
public: /* Ctors */
VMException(bool make_backtrace = true);
VMException(const char* reason, bool make_backtrace = true);
~VMException() { if(reason) free(reason); }
public: /* Interface */
void print_backtrace();
};
/**
* An assertion in the VM failed for the given +reason+.
* Only use this exception if there is no possible hope
* of handling the condition in Ruby-land. Otherwise, use:
*
* Exception::assertion_error(state, reason);
*/
class Assertion : public VMException {
public: /* Ctors */
/**
* Use Assertion::raise to throw an Assertion exception.
*/
Assertion(const char* reason) : VMException(reason) { }
public: /* Interface */
/**
* Throws an Assertion exception with explanation +reason+.
*/
static void raise(const char* reason);
};
class TypeError : public VMException {
public: /* Instance vars */
object_type type;
Object* object;
public: /* Ctors */
TypeError(object_type type, Object* obj, const char* reason = NULL)
: VMException(reason), type(type), object(obj) { }
public: /* Interface */
static void raise(object_type type, Object* obj, const char* reason = NULL);
};
/**
* Any exceptional VM condition that can be handled in Ruby-land
* should be propagated using a RubyException. However, instead
* of using this exception directly, use the appropriate static
* method on the builtin class Exception.
*/
class RubyException : public VMException {
public: /* Instance vars */
Exception* exception;
public: /* Ctors */
RubyException(Exception* exception, bool make_backtrace);
public: /* Interface */
static void raise(Exception* exception, bool make_backtrace = false);
/**
* Prints out the exception message and the Ruby backtrace.
* Also prints the VM backtrace if it was generated.
*/
void show(STATE);
};
};
#endif