Mercurial > projects > ldc
annotate tango/lib/common/tango/core/Exception.d @ 184:f5ca6bbbf1d7 trunk
[svn r200] Fixed: removed use of std.intrinsic.
Fixed: module info could potentially be masked by a previous reference, resulting in linking failure.
author | lindquist |
---|---|
date | Wed, 07 May 2008 22:01:59 +0200 |
parents | 44a95ac7368a |
children |
rev | line source |
---|---|
132 | 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 (C) 2005-2006 Sean Kelly, Kris Bell. All rights reserved. | |
6 * License: BSD style: $(LICENSE) | |
7 * Authors: Sean Kelly, Kris Bell | |
8 */ | |
9 module tango.core.Exception; | |
10 | |
11 | |
12 private | |
13 { | |
14 alias void function( char[] file, size_t line, char[] msg = null ) assertHandlerType; | |
15 alias TracedExceptionInfo function( void* ptr = null ) traceHandlerType; | |
16 | |
17 assertHandlerType assertHandler = null; | |
18 traceHandlerType traceHandler = null; | |
19 } | |
20 | |
21 | |
22 interface TracedExceptionInfo | |
23 { | |
24 int opApply( int delegate( inout char[] ) ); | |
25 } | |
26 | |
27 | |
28 //////////////////////////////////////////////////////////////////////////////// | |
29 /* | |
30 - Exception | |
31 - OutOfMemoryException | |
32 | |
33 - TracedException | |
34 - SwitchException | |
35 - AssertException | |
36 - ArrayBoundsException | |
37 - FinalizeException | |
38 | |
39 - PlatformException | |
40 - ProcessException | |
41 - ThreadException | |
42 - FiberException | |
43 - SyncException | |
44 - IOException | |
45 - SocketException | |
46 - SocketAcceptException | |
47 - AddressException | |
48 - HostException | |
49 - VfsException | |
50 - ClusterException | |
51 | |
52 - NoSuchElementException | |
53 - CorruptedIteratorException | |
54 | |
55 - IllegalArgumentException | |
56 - IllegalElementException | |
57 | |
58 - TextException | |
59 - RegexException | |
60 - LocaleException | |
61 - UnicodeException | |
62 | |
63 - PayloadException | |
64 */ | |
65 //////////////////////////////////////////////////////////////////////////////// | |
66 | |
67 | |
68 /** | |
69 * Thrown on an out of memory error. | |
70 */ | |
71 class OutOfMemoryException : Exception | |
72 { | |
73 this( char[] file, size_t line ) | |
74 { | |
75 super( "Memory allocation failed", file, line ); | |
76 } | |
77 | |
78 char[] toString() | |
79 { | |
80 return msg ? super.toString() : "Memory allocation failed"; | |
81 } | |
82 } | |
83 | |
84 | |
85 /** | |
86 * Stores a stack trace when thrown. | |
87 */ | |
88 class TracedException : Exception | |
89 { | |
90 this( char[] msg ) | |
91 { | |
92 super( msg ); | |
93 m_info = traceContext(); | |
94 } | |
95 | |
96 this( char[] msg, Exception e ) | |
97 { | |
98 super( msg, e ); | |
99 m_info = traceContext(); | |
100 } | |
101 | |
102 this( char[] msg, char[] file, size_t line ) | |
103 { | |
104 super( msg, file, line ); | |
105 m_info = traceContext(); | |
106 } | |
107 | |
108 char[] toString() | |
109 { | |
110 if( m_info is null ) | |
111 return super.toString(); | |
112 char[] buf = super.toString(); | |
113 buf ~= "\n----------------"; | |
114 foreach( line; m_info ) | |
115 buf ~= "\n" ~ line; | |
116 return buf; | |
117 } | |
118 | |
119 int opApply( int delegate( inout char[] buf ) dg ) | |
120 { | |
121 if( m_info is null ) | |
122 return 0; | |
123 return m_info.opApply( dg ); | |
124 } | |
125 | |
126 private: | |
127 TracedExceptionInfo m_info; | |
128 } | |
129 | |
130 | |
131 /** | |
132 * Base class for operating system or library exceptions. | |
133 */ | |
134 class PlatformException : TracedException | |
135 { | |
136 this( char[] msg ) | |
137 { | |
138 super( msg ); | |
139 } | |
140 } | |
141 | |
142 /** | |
143 * Thrown on an assert error. | |
144 */ | |
145 class AssertException : TracedException | |
146 { | |
147 this( char[] file, size_t line ) | |
148 { | |
149 super( "Assertion failure", file, line ); | |
150 } | |
151 | |
152 this( char[] msg, char[] file, size_t line ) | |
153 { | |
154 super( msg, file, line ); | |
155 } | |
156 } | |
157 | |
158 | |
159 /** | |
160 * Thrown on an array bounds error. | |
161 */ | |
162 class ArrayBoundsException : TracedException | |
163 { | |
164 this( char[] file, size_t line ) | |
165 { | |
166 super( "Array index out of bounds", file, line ); | |
167 } | |
168 } | |
169 | |
170 | |
171 /** | |
172 * Thrown on finalize error. | |
173 */ | |
174 class FinalizeException : TracedException | |
175 { | |
176 ClassInfo info; | |
177 | |
178 this( ClassInfo c, Exception e = null ) | |
179 { | |
180 super( "Finalization error", e ); | |
181 info = c; | |
182 } | |
183 | |
184 char[] toString() | |
185 { | |
186 //return "An exception was thrown while finalizing an instance of class " ~ info.name; | |
187 assert(0); | |
188 } | |
189 } | |
190 | |
191 | |
192 /** | |
193 * Thrown on a switch error. | |
194 */ | |
195 class SwitchException : TracedException | |
196 { | |
197 this( char[] file, size_t line ) | |
198 { | |
199 super( "No appropriate switch clause found", file, line ); | |
200 } | |
201 } | |
202 | |
203 | |
204 /** | |
205 * Represents a text processing error. | |
206 */ | |
207 class TextException : TracedException | |
208 { | |
209 this( char[] msg ) | |
210 { | |
211 super( msg ); | |
212 } | |
213 } | |
214 | |
215 /** | |
216 * Thrown on a unicode conversion error. | |
217 */ | |
218 class UnicodeException : TextException | |
219 { | |
220 size_t idx; | |
221 | |
222 this( char[] msg, size_t idx ) | |
223 { | |
224 super( msg ); | |
225 this.idx = idx; | |
226 } | |
227 } | |
228 | |
229 | |
230 /** | |
231 * Base class for thread exceptions. | |
232 */ | |
233 class ThreadException : PlatformException | |
234 { | |
235 this( char[] msg ) | |
236 { | |
237 super( msg ); | |
238 } | |
239 } | |
240 | |
241 | |
242 /** | |
243 * Base class for fiber exceptions. | |
244 */ | |
245 class FiberException : ThreadException | |
246 { | |
247 this( char[] msg ) | |
248 { | |
249 super( msg ); | |
250 } | |
251 } | |
252 | |
253 | |
254 /** | |
255 * Base class for synchronization exceptions. | |
256 */ | |
257 class SyncException : PlatformException | |
258 { | |
259 this( char[] msg ) | |
260 { | |
261 super( msg ); | |
262 } | |
263 } | |
264 | |
265 | |
266 | |
267 /** | |
268 * The basic exception thrown by the tango.io package. One should try to ensure | |
269 * that all Tango exceptions related to IO are derived from this one. | |
270 */ | |
271 class IOException : PlatformException | |
272 { | |
273 this( char[] msg ) | |
274 { | |
275 super( msg ); | |
276 } | |
277 } | |
278 | |
279 /** | |
280 * The basic exception thrown by the tango.io.vfs package. | |
281 */ | |
282 private class VfsException : IOException | |
283 { | |
284 this( char[] msg ) | |
285 { | |
286 super( msg ); | |
287 } | |
288 } | |
289 | |
290 /** | |
291 * The basic exception thrown by the tango.io.cluster package. | |
292 */ | |
293 private class ClusterException : IOException | |
294 { | |
295 this( char[] msg ) | |
296 { | |
297 super( msg ); | |
298 } | |
299 } | |
300 | |
301 /** | |
302 * Base class for socket exceptions. | |
303 */ | |
304 class SocketException : IOException | |
305 { | |
306 this( char[] msg ) | |
307 { | |
308 super( msg ); | |
309 } | |
310 } | |
311 | |
312 | |
313 /** | |
314 * Base class for exception thrown by an InternetHost. | |
315 */ | |
316 class HostException : IOException | |
317 { | |
318 this( char[] msg ) | |
319 { | |
320 super( msg ); | |
321 } | |
322 } | |
323 | |
324 | |
325 /** | |
326 * Base class for exceptiond thrown by an Address. | |
327 */ | |
328 class AddressException : IOException | |
329 { | |
330 this( char[] msg ) | |
331 { | |
332 super( msg ); | |
333 } | |
334 } | |
335 | |
336 | |
337 /** | |
338 * Thrown when a socket failed to accept an incoming connection. | |
339 */ | |
340 class SocketAcceptException : SocketException | |
341 { | |
342 this( char[] msg ) | |
343 { | |
344 super( msg ); | |
345 } | |
346 } | |
347 | |
348 | |
349 /** | |
350 * Thrown on a process error. | |
351 */ | |
352 class ProcessException : PlatformException | |
353 { | |
354 this( char[] msg ) | |
355 { | |
356 super( msg ); | |
357 } | |
358 } | |
359 | |
360 | |
361 /** | |
362 * Base class for regluar expression exceptions. | |
363 */ | |
364 class RegexException : TextException | |
365 { | |
366 this( char[] msg ) | |
367 { | |
368 super( msg ); | |
369 } | |
370 } | |
371 | |
372 | |
373 /** | |
374 * Base class for locale exceptions. | |
375 */ | |
376 class LocaleException : TextException | |
377 { | |
378 this( char[] msg ) | |
379 { | |
380 super( msg ); | |
381 } | |
382 } | |
383 | |
384 | |
385 /** | |
386 * RegistryException is thrown when the NetworkRegistry encounters a | |
387 * problem during proxy registration, or when it sees an unregistered | |
388 * guid. | |
389 */ | |
390 class RegistryException : TracedException | |
391 { | |
392 this( char[] msg ) | |
393 { | |
394 super( msg ); | |
395 } | |
396 } | |
397 | |
398 | |
399 /** | |
400 * Thrown when an illegal argument is encountered. | |
401 */ | |
402 class IllegalArgumentException : TracedException | |
403 { | |
404 this( char[] msg ) | |
405 { | |
406 super( msg ); | |
407 } | |
408 } | |
409 | |
410 | |
411 /** | |
412 * | |
413 * IllegalElementException is thrown by Collection methods | |
414 * that add (or replace) elements (and/or keys) when their | |
415 * arguments are null or do not pass screeners. | |
416 * | |
417 */ | |
418 class IllegalElementException : IllegalArgumentException | |
419 { | |
420 this( char[] msg ) | |
421 { | |
422 super( msg ); | |
423 } | |
424 } | |
425 | |
426 | |
427 /** | |
428 * Thrown on past-the-end errors by iterators and containers. | |
429 */ | |
430 class NoSuchElementException : TracedException | |
431 { | |
432 this( char[] msg ) | |
433 { | |
434 super( msg ); | |
435 } | |
436 } | |
437 | |
438 | |
439 /** | |
440 * Thrown when a corrupt iterator is detected. | |
441 */ | |
442 class CorruptedIteratorException : NoSuchElementException | |
443 { | |
444 this( char[] msg ) | |
445 { | |
446 super( msg ); | |
447 } | |
448 } | |
449 | |
450 | |
451 //////////////////////////////////////////////////////////////////////////////// | |
452 // Overrides | |
453 //////////////////////////////////////////////////////////////////////////////// | |
454 | |
455 | |
456 /** | |
457 * Overrides the default assert hander with a user-supplied version. | |
458 * | |
459 * Params: | |
460 * h = The new assert handler. Set to null to use the default handler. | |
461 */ | |
462 void setAssertHandler( assertHandlerType h ) | |
463 { | |
464 assertHandler = h; | |
465 } | |
466 | |
467 | |
468 /** | |
469 * Overrides the default trace hander with a user-supplied version. | |
470 * | |
471 * Params: | |
472 * h = The new trace handler. Set to null to use the default handler. | |
473 */ | |
474 void setTraceHandler( traceHandlerType h ) | |
475 { | |
476 traceHandler = h; | |
477 } | |
478 | |
479 | |
480 //////////////////////////////////////////////////////////////////////////////// | |
481 // Overridable Callbacks | |
482 //////////////////////////////////////////////////////////////////////////////// | |
483 | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
484 private extern(C) int printf(char*,...); |
132 | 485 |
486 /** | |
487 * A callback for assert errors in D. The user-supplied assert handler will | |
488 * be called if one has been supplied, otherwise an AssertException will be | |
489 * thrown. | |
490 * | |
491 * Params: | |
492 * file = The name of the file that signaled this error. | |
493 * line = The line number on which this error occurred. | |
494 */ | |
495 extern (C) void onAssertError( char[] file, size_t line ) | |
496 { | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
497 printf("Assertion failed:\n"); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
498 printf("%.*s(%lu)\n", file.length, file.ptr, line); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
499 if( assertHandler is null ) { |
132 | 500 throw new AssertException( file, line ); |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
501 } |
132 | 502 assertHandler( file, line ); |
503 } | |
504 | |
505 | |
506 /** | |
507 * A callback for assert errors in D. The user-supplied assert handler will | |
508 * be called if one has been supplied, otherwise an AssertException will be | |
509 * thrown. | |
510 * | |
511 * Params: | |
512 * file = The name of the file that signaled this error. | |
513 * line = The line number on which this error occurred. | |
514 * msg = An error message supplied by the user. | |
515 */ | |
516 extern (C) void onAssertErrorMsg( char[] file, size_t line, char[] msg ) | |
517 { | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
518 printf("Assertion failed:\n"); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
519 printf("%.*s(%lu):\n", file.length, file.ptr, line); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
520 printf("%.*s\n", msg.length, msg.ptr); |
132 | 521 if( assertHandler is null ) |
522 throw new AssertException( msg, file, line ); | |
523 assertHandler( file, line, msg ); | |
524 } | |
525 | |
526 | |
527 /** | |
528 * This function will be called when a TracedException is constructed. The | |
529 * user-supplied trace handler will be called if one has been supplied, | |
530 * otherwise no trace will be generated. | |
531 * | |
532 * Params: | |
533 * ptr = A pointer to the location from which to generate the trace, or null | |
534 * if the trace should be generated from within the trace handler | |
535 * itself. | |
536 * | |
537 * Returns: | |
538 * An object describing the current calling context or null if no handler is | |
539 * supplied. | |
540 */ | |
541 TracedExceptionInfo traceContext( void* ptr = null ) | |
542 { | |
543 if( traceHandler is null ) | |
544 return null; | |
545 return traceHandler( ptr ); | |
546 } | |
547 | |
548 //////////////////////////////////////////////////////////////////////////////// | |
549 // Internal Error Callbacks | |
550 //////////////////////////////////////////////////////////////////////////////// | |
551 | |
552 | |
553 /** | |
554 * A callback for array bounds errors in D. An ArrayBoundsException will be | |
555 * thrown. | |
556 * | |
557 * Params: | |
558 * file = The name of the file that signaled this error. | |
559 * line = The line number on which this error occurred. | |
560 * | |
561 * Throws: | |
562 * ArrayBoundsException. | |
563 */ | |
564 extern (C) void onArrayBoundsError( char[] file, size_t line ) | |
565 { | |
566 throw new ArrayBoundsException( file, line ); | |
567 } | |
568 | |
569 | |
570 /** | |
571 * A callback for finalize errors in D. A FinalizeException will be thrown. | |
572 * | |
573 * Params: | |
574 * e = The exception thrown during finalization. | |
575 * | |
576 * Throws: | |
577 * FinalizeException. | |
578 */ | |
579 extern (C) void onFinalizeError( ClassInfo info, Exception ex ) | |
580 { | |
581 throw new FinalizeException( info, ex ); | |
582 } | |
583 | |
584 | |
585 /** | |
586 * A callback for out of memory errors in D. An OutOfMemoryException will be | |
587 * thrown. | |
588 * | |
589 * Throws: | |
590 * OutOfMemoryException. | |
591 */ | |
592 extern (C) void onOutOfMemoryError() | |
593 { | |
594 // NOTE: Since an out of memory condition exists, no allocation must occur | |
595 // while generating this object. | |
596 throw cast(OutOfMemoryException) cast(void*) OutOfMemoryException.classinfo.init; | |
597 } | |
598 | |
599 | |
600 /** | |
601 * A callback for switch errors in D. A SwitchException will be thrown. | |
602 * | |
603 * Params: | |
604 * file = The name of the file that signaled this error. | |
605 * line = The line number on which this error occurred. | |
606 * | |
607 * Throws: | |
608 * SwitchException. | |
609 */ | |
610 extern (C) void onSwitchError( char[] file, size_t line ) | |
611 { | |
612 throw new SwitchException( file, line ); | |
613 } | |
614 | |
615 | |
616 /** | |
617 * A callback for unicode errors in D. A UnicodeException will be thrown. | |
618 * | |
619 * Params: | |
620 * msg = Information about the error. | |
621 * idx = String index where this error was detected. | |
622 * | |
623 * Throws: | |
624 * UnicodeException. | |
625 */ | |
626 extern (C) void onUnicodeError( char[] msg, size_t idx ) | |
627 { | |
628 throw new UnicodeException( msg, idx ); | |
629 } |