| 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 |
version = SocketSpecifics; // TODO: remove this before v1.0 |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
private |
|---|
| 15 |
{ |
|---|
| 16 |
alias void function( char[] file, size_t line, char[] msg = null ) assertHandlerType; |
|---|
| 17 |
|
|---|
| 18 |
assertHandlerType assertHandler = null; |
|---|
| 19 |
} |
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 23 |
/* |
|---|
| 24 |
- Exception |
|---|
| 25 |
- OutOfMemoryException |
|---|
| 26 |
- SwitchException |
|---|
| 27 |
- AssertException |
|---|
| 28 |
- ArrayBoundsException |
|---|
| 29 |
- FinalizeException |
|---|
| 30 |
|
|---|
| 31 |
- PlatformException |
|---|
| 32 |
- ProcessException |
|---|
| 33 |
- ThreadException |
|---|
| 34 |
- FiberException |
|---|
| 35 |
- SyncException |
|---|
| 36 |
- IOException |
|---|
| 37 |
- SocketException |
|---|
| 38 |
- VfsException |
|---|
| 39 |
- ClusterException |
|---|
| 40 |
|
|---|
| 41 |
- NoSuchElementException |
|---|
| 42 |
- CorruptedIteratorException |
|---|
| 43 |
|
|---|
| 44 |
- IllegalArgumentException |
|---|
| 45 |
- IllegalElementException |
|---|
| 46 |
|
|---|
| 47 |
- TextException |
|---|
| 48 |
- XmlException |
|---|
| 49 |
- RegexException |
|---|
| 50 |
- LocaleException |
|---|
| 51 |
- UnicodeException |
|---|
| 52 |
|
|---|
| 53 |
- PayloadException |
|---|
| 54 |
*/ |
|---|
| 55 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
/** |
|---|
| 59 |
* Thrown on an out of memory error. |
|---|
| 60 |
*/ |
|---|
| 61 |
class OutOfMemoryException : Exception |
|---|
| 62 |
{ |
|---|
| 63 |
this( char[] file, long line ) |
|---|
| 64 |
{ |
|---|
| 65 |
super( "Memory allocation failed", file, line ); |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
char[] toString() |
|---|
| 69 |
{ |
|---|
| 70 |
return msg ? super.toString() : "Memory allocation failed"; |
|---|
| 71 |
} |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
/** |
|---|
| 76 |
* Base class for operating system or library exceptions. |
|---|
| 77 |
*/ |
|---|
| 78 |
class PlatformException : Exception |
|---|
| 79 |
{ |
|---|
| 80 |
this( char[] msg ) |
|---|
| 81 |
{ |
|---|
| 82 |
super( msg ); |
|---|
| 83 |
} |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
/** |
|---|
| 87 |
* Thrown on an assert error. |
|---|
| 88 |
*/ |
|---|
| 89 |
class AssertException : Exception |
|---|
| 90 |
{ |
|---|
| 91 |
this( char[] file, long line ) |
|---|
| 92 |
{ |
|---|
| 93 |
super( "Assertion failure", file, line ); |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
this( char[] msg, char[] file, long line ) |
|---|
| 97 |
{ |
|---|
| 98 |
super( msg, file, line ); |
|---|
| 99 |
} |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
/** |
|---|
| 104 |
* Thrown on an array bounds error. |
|---|
| 105 |
*/ |
|---|
| 106 |
class ArrayBoundsException : Exception |
|---|
| 107 |
{ |
|---|
| 108 |
this( char[] file, long line ) |
|---|
| 109 |
{ |
|---|
| 110 |
super( "Array index out of bounds", file, line ); |
|---|
| 111 |
} |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
/** |
|---|
| 116 |
* Thrown on finalize error. |
|---|
| 117 |
*/ |
|---|
| 118 |
class FinalizeException : Exception |
|---|
| 119 |
{ |
|---|
| 120 |
ClassInfo info; |
|---|
| 121 |
|
|---|
| 122 |
this( ClassInfo c, Exception e ) |
|---|
| 123 |
{ |
|---|
| 124 |
super( "Finalization error", e ); |
|---|
| 125 |
info = c; |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
override char[] toString() |
|---|
| 129 |
{ |
|---|
| 130 |
auto other = super.next ? super.next.toString : "unknown"; |
|---|
| 131 |
return "An exception was thrown while finalizing an instance of class " ~ info.name ~ " :: "~other; |
|---|
| 132 |
} |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
/** |
|---|
| 137 |
* Thrown on a switch error. |
|---|
| 138 |
*/ |
|---|
| 139 |
class SwitchException : Exception |
|---|
| 140 |
{ |
|---|
| 141 |
this( char[] file, long line ) |
|---|
| 142 |
{ |
|---|
| 143 |
super( "No appropriate switch clause found", file, line ); |
|---|
| 144 |
} |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
/** |
|---|
| 149 |
* Represents a text processing error. |
|---|
| 150 |
*/ |
|---|
| 151 |
class TextException : Exception |
|---|
| 152 |
{ |
|---|
| 153 |
this( char[] msg ) |
|---|
| 154 |
{ |
|---|
| 155 |
super( msg ); |
|---|
| 156 |
} |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
/** |
|---|
| 160 |
* Thrown on a unicode conversion error. |
|---|
| 161 |
*/ |
|---|
| 162 |
class UnicodeException : TextException |
|---|
| 163 |
{ |
|---|
| 164 |
size_t idx; |
|---|
| 165 |
|
|---|
| 166 |
this( char[] msg, size_t idx ) |
|---|
| 167 |
{ |
|---|
| 168 |
super( msg ); |
|---|
| 169 |
this.idx = idx; |
|---|
| 170 |
} |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
/** |
|---|
| 175 |
* Base class for thread exceptions. |
|---|
| 176 |
*/ |
|---|
| 177 |
class ThreadException : PlatformException |
|---|
| 178 |
{ |
|---|
| 179 |
this( char[] msg ) |
|---|
| 180 |
{ |
|---|
| 181 |
super( msg ); |
|---|
| 182 |
} |
|---|
| 183 |
} |
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
/** |
|---|
| 187 |
* Base class for fiber exceptions. |
|---|
| 188 |
*/ |
|---|
| 189 |
class FiberException : ThreadException |
|---|
| 190 |
{ |
|---|
| 191 |
this( char[] msg ) |
|---|
| 192 |
{ |
|---|
| 193 |
super( msg ); |
|---|
| 194 |
} |
|---|
| 195 |
} |
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
/** |
|---|
| 199 |
* Base class for synchronization exceptions. |
|---|
| 200 |
*/ |
|---|
| 201 |
class SyncException : PlatformException |
|---|
| 202 |
{ |
|---|
| 203 |
this( char[] msg ) |
|---|
| 204 |
{ |
|---|
| 205 |
super( msg ); |
|---|
| 206 |
} |
|---|
| 207 |
} |
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
/** |
|---|
| 212 |
* The basic exception thrown by the tango.io package. One should try to ensure |
|---|
| 213 |
* that all Tango exceptions related to IO are derived from this one. |
|---|
| 214 |
*/ |
|---|
| 215 |
class IOException : PlatformException |
|---|
| 216 |
{ |
|---|
| 217 |
this( char[] msg ) |
|---|
| 218 |
{ |
|---|
| 219 |
super( msg ); |
|---|
| 220 |
} |
|---|
| 221 |
} |
|---|
| 222 |
|
|---|
| 223 |
/** |
|---|
| 224 |
* The basic exception thrown by the tango.io.vfs package. |
|---|
| 225 |
*/ |
|---|
| 226 |
class VfsException : IOException |
|---|
| 227 |
{ |
|---|
| 228 |
this( char[] msg ) |
|---|
| 229 |
{ |
|---|
| 230 |
super( msg ); |
|---|
| 231 |
} |
|---|
| 232 |
} |
|---|
| 233 |
|
|---|
| 234 |
/** |
|---|
| 235 |
* The basic exception thrown by the tango.io.cluster package. |
|---|
| 236 |
*/ |
|---|
| 237 |
class ClusterException : IOException |
|---|
| 238 |
{ |
|---|
| 239 |
this( char[] msg ) |
|---|
| 240 |
{ |
|---|
| 241 |
super( msg ); |
|---|
| 242 |
} |
|---|
| 243 |
} |
|---|
| 244 |
|
|---|
| 245 |
/** |
|---|
| 246 |
* Base class for socket exceptions. |
|---|
| 247 |
*/ |
|---|
| 248 |
class SocketException : IOException |
|---|
| 249 |
{ |
|---|
| 250 |
this( char[] msg ) |
|---|
| 251 |
{ |
|---|
| 252 |
super( msg ); |
|---|
| 253 |
} |
|---|
| 254 |
} |
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
version (SocketSpecifics) |
|---|
| 258 |
{ |
|---|
| 259 |
/** |
|---|
| 260 |
* Base class for exception thrown by an InternetHost. |
|---|
| 261 |
*/ |
|---|
| 262 |
class HostException : IOException |
|---|
| 263 |
{ |
|---|
| 264 |
this( char[] msg ) |
|---|
| 265 |
{ |
|---|
| 266 |
super( msg ); |
|---|
| 267 |
} |
|---|
| 268 |
} |
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
/** |
|---|
| 272 |
* Base class for exceptiond thrown by an Address. |
|---|
| 273 |
*/ |
|---|
| 274 |
class AddressException : IOException |
|---|
| 275 |
{ |
|---|
| 276 |
this( char[] msg ) |
|---|
| 277 |
{ |
|---|
| 278 |
super( msg ); |
|---|
| 279 |
} |
|---|
| 280 |
} |
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
/** |
|---|
| 284 |
* Thrown when a socket failed to accept an incoming connection. |
|---|
| 285 |
*/ |
|---|
| 286 |
class SocketAcceptException : SocketException |
|---|
| 287 |
{ |
|---|
| 288 |
this( char[] msg ) |
|---|
| 289 |
{ |
|---|
| 290 |
super( msg ); |
|---|
| 291 |
} |
|---|
| 292 |
} |
|---|
| 293 |
} |
|---|
| 294 |
|
|---|
| 295 |
/** |
|---|
| 296 |
* Thrown on a process error. |
|---|
| 297 |
*/ |
|---|
| 298 |
class ProcessException : PlatformException |
|---|
| 299 |
{ |
|---|
| 300 |
this( char[] msg ) |
|---|
| 301 |
{ |
|---|
| 302 |
super( msg ); |
|---|
| 303 |
} |
|---|
| 304 |
} |
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
/** |
|---|
| 308 |
* Base class for regluar expression exceptions. |
|---|
| 309 |
*/ |
|---|
| 310 |
class RegexException : TextException |
|---|
| 311 |
{ |
|---|
| 312 |
this( char[] msg ) |
|---|
| 313 |
{ |
|---|
| 314 |
super( msg ); |
|---|
| 315 |
} |
|---|
| 316 |
} |
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 |
/** |
|---|
| 320 |
* Base class for locale exceptions. |
|---|
| 321 |
*/ |
|---|
| 322 |
class LocaleException : TextException |
|---|
| 323 |
{ |
|---|
| 324 |
this( char[] msg ) |
|---|
| 325 |
{ |
|---|
| 326 |
super( msg ); |
|---|
| 327 |
} |
|---|
| 328 |
} |
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
/** |
|---|
| 332 |
* Base class for XML exceptions. |
|---|
| 333 |
*/ |
|---|
| 334 |
class XmlException : TextException |
|---|
| 335 |
{ |
|---|
| 336 |
this( char[] msg ) |
|---|
| 337 |
{ |
|---|
| 338 |
super( msg ); |
|---|
| 339 |
} |
|---|
| 340 |
} |
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
/** |
|---|
| 344 |
* RegistryException is thrown when the NetworkRegistry encounters a |
|---|
| 345 |
* problem during proxy registration, or when it sees an unregistered |
|---|
| 346 |
* guid. |
|---|
| 347 |
*/ |
|---|
| 348 |
class RegistryException : Exception |
|---|
| 349 |
{ |
|---|
| 350 |
this( char[] msg ) |
|---|
| 351 |
{ |
|---|
| 352 |
super( msg ); |
|---|
| 353 |
} |
|---|
| 354 |
} |
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 |
/** |
|---|
| 358 |
* Thrown when an illegal argument is encountered. |
|---|
| 359 |
*/ |
|---|
| 360 |
class IllegalArgumentException : Exception |
|---|
| 361 |
{ |
|---|
| 362 |
this( char[] msg ) |
|---|
| 363 |
{ |
|---|
| 364 |
super( msg ); |
|---|
| 365 |
} |
|---|
| 366 |
} |
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 |
/** |
|---|
| 370 |
* |
|---|
| 371 |
* IllegalElementException is thrown by Collection methods |
|---|
| 372 |
* that add (or replace) elements (and/or keys) when their |
|---|
| 373 |
* arguments are null or do not pass screeners. |
|---|
| 374 |
* |
|---|
| 375 |
*/ |
|---|
| 376 |
class IllegalElementException : IllegalArgumentException |
|---|
| 377 |
{ |
|---|
| 378 |
this( char[] msg ) |
|---|
| 379 |
{ |
|---|
| 380 |
super( msg ); |
|---|
| 381 |
} |
|---|
| 382 |
} |
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
/** |
|---|
| 386 |
* Thrown on past-the-end errors by iterators and containers. |
|---|
| 387 |
*/ |
|---|
| 388 |
class NoSuchElementException : Exception |
|---|
| 389 |
{ |
|---|
| 390 |
this( char[] msg ) |
|---|
| 391 |
{ |
|---|
| 392 |
super( msg ); |
|---|
| 393 |
} |
|---|
| 394 |
} |
|---|
| 395 |
|
|---|
| 396 |
|
|---|
| 397 |
/** |
|---|
| 398 |
* Thrown when a corrupt iterator is detected. |
|---|
| 399 |
*/ |
|---|
| 400 |
class CorruptedIteratorException : NoSuchElementException |
|---|
| 401 |
{ |
|---|
| 402 |
this( char[] msg ) |
|---|
| 403 |
{ |
|---|
| 404 |
super( msg ); |
|---|
| 405 |
} |
|---|
| 406 |
} |
|---|
| 407 |
|
|---|
| 408 |
|
|---|
| 409 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 410 |
// Overrides |
|---|
| 411 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 412 |
|
|---|
| 413 |
|
|---|
| 414 |
/** |
|---|
| 415 |
* Overrides the default assert hander with a user-supplied version. |
|---|
| 416 |
* |
|---|
| 417 |
* Params: |
|---|
| 418 |
* h = The new assert handler. Set to null to use the default handler. |
|---|
| 419 |
*/ |
|---|
| 420 |
void setAssertHandler( assertHandlerType h ) |
|---|
| 421 |
{ |
|---|
| 422 |
assertHandler = h; |
|---|
| 423 |
} |
|---|
| 424 |
|
|---|
| 425 |
|
|---|
| 426 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 427 |
// Overridable Callbacks |
|---|
| 428 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 429 |
|
|---|
| 430 |
|
|---|
| 431 |
/** |
|---|
| 432 |
* A callback for assert errors in D. The user-supplied assert handler will |
|---|
| 433 |
* be called if one has been supplied, otherwise an AssertException will be |
|---|
| 434 |
* thrown. |
|---|
| 435 |
* |
|---|
| 436 |
* Params: |
|---|
| 437 |
* file = The name of the file that signaled this error. |
|---|
| 438 |
* line = The line number on which this error occurred. |
|---|
| 439 |
*/ |
|---|
| 440 |
extern (C) void onAssertError( char[] file, size_t line ) |
|---|
| 441 |
{ |
|---|
| 442 |
if( assertHandler is null ) |
|---|
| 443 |
throw new AssertException( file, cast(long)line ); |
|---|
| 444 |
assertHandler( file, line ); |
|---|
| 445 |
} |
|---|
| 446 |
|
|---|
| 447 |
|
|---|
| 448 |
/** |
|---|
| 449 |
* A callback for assert errors in D. The user-supplied assert handler will |
|---|
| 450 |
* be called if one has been supplied, otherwise an AssertException will be |
|---|
| 451 |
* thrown. |
|---|
| 452 |
* |
|---|
| 453 |
* Params: |
|---|
| 454 |
* file = The name of the file that signaled this error. |
|---|
| 455 |
* line = The line number on which this error occurred. |
|---|
| 456 |
* msg = An error message supplied by the user. |
|---|
| 457 |
*/ |
|---|
| 458 |
extern (C) void onAssertErrorMsg( char[] file, size_t line, char[] msg ) |
|---|
| 459 |
{ |
|---|
| 460 |
if( assertHandler is null ) |
|---|
| 461 |
throw new AssertException( msg, file, cast(long)line ); |
|---|
| 462 |
assertHandler( file, line, msg ); |
|---|
| 463 |
} |
|---|
| 464 |
|
|---|
| 465 |
|
|---|
| 466 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 467 |
// Internal Error Callbacks |
|---|
| 468 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 469 |
|
|---|
| 470 |
|
|---|
| 471 |
/** |
|---|
| 472 |
* A callback for array bounds errors in D. An ArrayBoundsException will be |
|---|
| 473 |
* thrown. |
|---|
| 474 |
* |
|---|
| 475 |
* Params: |
|---|
| 476 |
* file = The name of the file that signaled this error. |
|---|
| 477 |
* line = The line number on which this error occurred. |
|---|
| 478 |
* |
|---|
| 479 |
* Throws: |
|---|
| 480 |
* ArrayBoundsException. |
|---|
| 481 |
*/ |
|---|
| 482 |
extern (C) void onArrayBoundsError( char[] file, size_t line ) |
|---|
| 483 |
{ |
|---|
| 484 |
throw new ArrayBoundsException( file, cast(long)line ); |
|---|
| 485 |
} |
|---|
| 486 |
|
|---|
| 487 |
|
|---|
| 488 |
/** |
|---|
| 489 |
* A callback for finalize errors in D. A FinalizeException will be thrown. |
|---|
| 490 |
* |
|---|
| 491 |
* Params: |
|---|
| 492 |
* e = The exception thrown during finalization. |
|---|
| 493 |
* |
|---|
| 494 |
* Throws: |
|---|
| 495 |
* FinalizeException. |
|---|
| 496 |
*/ |
|---|
| 497 |
extern (C) void onFinalizeError( ClassInfo info, Exception ex ) |
|---|
| 498 |
{ |
|---|
| 499 |
throw new FinalizeException( info, ex ); |
|---|
| 500 |
} |
|---|
| 501 |
|
|---|
| 502 |
|
|---|
| 503 |
/** |
|---|
| 504 |
* A callback for out of memory errors in D. An OutOfMemoryException will be |
|---|
| 505 |
* thrown. |
|---|
| 506 |
* |
|---|
| 507 |
* Throws: |
|---|
| 508 |
* OutOfMemoryException. |
|---|
| 509 |
*/ |
|---|
| 510 |
extern (C) void onOutOfMemoryError() |
|---|
| 511 |
{ |
|---|
| 512 |
// NOTE: Since an out of memory condition exists, no allocation must occur |
|---|
| 513 |
// while generating this object. |
|---|
| 514 |
throw cast(OutOfMemoryException) cast(void*) OutOfMemoryException.classinfo.init; |
|---|
| 515 |
} |
|---|
| 516 |
|
|---|
| 517 |
|
|---|
| 518 |
/** |
|---|
| 519 |
* A callback for switch errors in D. A SwitchException will be thrown. |
|---|
| 520 |
* |
|---|
| 521 |
* Params: |
|---|
| 522 |
* file = The name of the file that signaled this error. |
|---|
| 523 |
* line = The line number on which this error occurred. |
|---|
| 524 |
* |
|---|
| 525 |
* Throws: |
|---|
| 526 |
* SwitchException. |
|---|
| 527 |
*/ |
|---|
| 528 |
extern (C) void onSwitchError( char[] file, size_t line ) |
|---|
| 529 |
{ |
|---|
| 530 |
throw new SwitchException( file, cast(long)line ); |
|---|
| 531 |
} |
|---|
| 532 |
|
|---|
| 533 |
|
|---|
| 534 |
/** |
|---|
| 535 |
* A callback for unicode errors in D. A UnicodeException will be thrown. |
|---|
| 536 |
* |
|---|
| 537 |
* Params: |
|---|
| 538 |
* msg = Information about the error. |
|---|
| 539 |
* idx = String index where this error was detected. |
|---|
| 540 |
* |
|---|
| 541 |
* Throws: |
|---|
| 542 |
* UnicodeException. |
|---|
| 543 |
*/ |
|---|
| 544 |
extern (C) void onUnicodeError( char[] msg, size_t idx ) |
|---|
| 545 |
{ |
|---|
| 546 |
throw new UnicodeException( msg, idx ); |
|---|
| 547 |
} |
|---|