comparison druntime/src/common/core/exception.d @ 1458:e0b2d67cfe7c

Added druntime (this should be removed once it works).
author Robert Clipsham <robert@octarineparrot.com>
date Tue, 02 Jun 2009 17:43:06 +0100
parents
children
comparison
equal deleted inserted replaced
1456:7b218ec1044f 1458:e0b2d67cfe7c
1 /**
2 * The exception module defines all system-level exceptions and provides a
3 * mechanism to alter system-level error handling.
4 *
5 * Copyright: Copyright Sean Kelly 2005 - 2009.
6 * License: <a href="http://www.boost.org/LICENSE_1_0.txt>Boost License 1.0</a>.
7 * Authors: Sean Kelly
8 *
9 * Copyright Sean Kelly 2005 - 2009.
10 * Distributed under the Boost Software License, Version 1.0.
11 * (See accompanying file LICENSE_1_0.txt or copy at
12 * http://www.boost.org/LICENSE_1_0.txt)
13 */
14 module core.exception;
15
16
17 private
18 {
19 alias void function( string file, size_t line, string msg = null ) assertHandlerType;
20
21 // Note that this is set on a per-thread basis (should it be global?)
22 assertHandlerType assertHandler = null;
23 }
24
25
26 /**
27 * Thrown on a range error.
28 */
29 class RangeError : Error
30 {
31 this( string file, size_t line )
32 {
33 super( "Range violation", file, line );
34 }
35 }
36
37
38 /**
39 * Thrown on an assert error.
40 */
41 class AssertError : Error
42 {
43 this( string file, size_t line )
44 {
45 super( "Assertion failure", file, line );
46 }
47
48 this( string msg, string file, size_t line )
49 {
50 super( msg, file, line );
51 }
52 }
53
54
55 /**
56 * Thrown on finalize error.
57 */
58 class FinalizeError : Error
59 {
60 ClassInfo info;
61
62 this( ClassInfo c, Exception e = null )
63 {
64 super( "Finalization error", e );
65 info = c;
66 }
67
68 override string toString()
69 {
70 return "An exception was thrown while finalizing an instance of class " ~ info.name;
71 }
72 }
73
74
75 /**
76 * Thrown on hidden function error.
77 */
78 class HiddenFuncError : Error
79 {
80 this( ClassInfo ci )
81 {
82 super( "Hidden method called for " ~ ci.name );
83 }
84 }
85
86
87 /**
88 * Thrown on an out of memory error.
89 */
90 class OutOfMemoryError : Error
91 {
92 this( string file, size_t line )
93 {
94 super( "Memory allocation failed", file, line );
95 }
96
97 override string toString()
98 {
99 return msg ? super.toString() : "Memory allocation failed";
100 }
101 }
102
103
104 /**
105 * Thrown on a switch error.
106 */
107 class SwitchError : Error
108 {
109 this( string file, size_t line )
110 {
111 super( "No appropriate switch clause found", file, line );
112 }
113 }
114
115
116 /**
117 * Thrown on a unicode conversion error.
118 */
119 class UnicodeException : Exception
120 {
121 size_t idx;
122
123 this( string msg, size_t idx )
124 {
125 super( msg );
126 this.idx = idx;
127 }
128 }
129
130
131 ///////////////////////////////////////////////////////////////////////////////
132 // Overrides
133 ///////////////////////////////////////////////////////////////////////////////
134
135
136 /**
137 * Overrides the default assert hander with a user-supplied version.
138 *
139 * Params:
140 * h = The new assert handler. Set to null to use the default handler.
141 */
142 void setAssertHandler( assertHandlerType h )
143 {
144 assertHandler = h;
145 }
146
147
148 ///////////////////////////////////////////////////////////////////////////////
149 // Overridable Callbacks
150 ///////////////////////////////////////////////////////////////////////////////
151
152
153 /**
154 * A callback for assert errors in D. The user-supplied assert handler will
155 * be called if one has been supplied, otherwise an AssertError will be thrown.
156 *
157 * Params:
158 * file = The name of the file that signaled this error.
159 * line = The line number on which this error occurred.
160 */
161 extern (C) void onAssertError( string file, size_t line )
162 {
163 if( assertHandler is null )
164 throw new AssertError( file, line );
165 assertHandler( file, line );
166 }
167
168
169 /**
170 * A callback for assert errors in D. The user-supplied assert handler will
171 * be called if one has been supplied, otherwise an AssertError will be thrown.
172 *
173 * Params:
174 * file = The name of the file that signaled this error.
175 * line = The line number on which this error occurred.
176 * msg = An error message supplied by the user.
177 */
178 extern (C) void onAssertErrorMsg( string file, size_t line, string msg )
179 {
180 if( assertHandler is null )
181 throw new AssertError( msg, file, line );
182 assertHandler( file, line, msg );
183 }
184
185
186 ///////////////////////////////////////////////////////////////////////////////
187 // Internal Error Callbacks
188 ///////////////////////////////////////////////////////////////////////////////
189
190
191 /**
192 * A callback for array bounds errors in D. A RangeError will be thrown.
193 *
194 * Params:
195 * file = The name of the file that signaled this error.
196 * line = The line number on which this error occurred.
197 *
198 * Throws:
199 * RangeError.
200 */
201 extern (C) void onRangeError( string file, size_t line )
202 {
203 throw new RangeError( file, line );
204 }
205
206
207 /**
208 * A callback for finalize errors in D. A FinalizeError will be thrown.
209 *
210 * Params:
211 * e = The exception thrown during finalization.
212 *
213 * Throws:
214 * FinalizeError.
215 */
216 extern (C) void onFinalizeError( ClassInfo info, Exception ex )
217 {
218 throw new FinalizeError( info, ex );
219 }
220
221
222 /**
223 * A callback for hidden function errors in D. A HiddenFuncError will be
224 * thrown.
225 *
226 * Throws:
227 * HiddenFuncError.
228 */
229 extern (C) void onHiddenFuncError( Object o )
230 {
231 throw new HiddenFuncError( o.classinfo );
232 }
233
234
235 /**
236 * A callback for out of memory errors in D. An OutOfMemoryError will be
237 * thrown.
238 *
239 * Throws:
240 * OutOfMemoryError.
241 */
242 extern (C) void onOutOfMemoryError()
243 {
244 // NOTE: Since an out of memory condition exists, no allocation must occur
245 // while generating this object.
246 throw cast(OutOfMemoryError) cast(void*) OutOfMemoryError.classinfo.init;
247 }
248
249
250 /**
251 * A callback for switch errors in D. A SwitchError will be thrown.
252 *
253 * Params:
254 * file = The name of the file that signaled this error.
255 * line = The line number on which this error occurred.
256 *
257 * Throws:
258 * SwitchError.
259 */
260 extern (C) void onSwitchError( string file, size_t line )
261 {
262 throw new SwitchError( file, line );
263 }
264
265
266 /**
267 * A callback for unicode errors in D. A UnicodeException will be thrown.
268 *
269 * Params:
270 * msg = Information about the error.
271 * idx = String index where this error was detected.
272 *
273 * Throws:
274 * UnicodeException.
275 */
276 extern (C) void onUnicodeError( string msg, size_t idx )
277 {
278 throw new UnicodeException( msg, idx );
279 }