With this statement you can throw an exception. This exception can be caught with the try and catch statements and can change the behaviour of the program sensible at runtime.
Note: Exceptions are much slower than return codes, but usually they are more applicable in most situations.
You can use the implemented exceptions, but sooner or later it is handy to declare your own exception:
class myPersonalCriticalError : Exception {
this(char[] msg) {
super(msg);
}
}
|
This way you define a new class called myPersonalCriticalError which can be used to throw an exception:
try {
throw new myPersonalCriticalError("This error MUST be!");
}
|
The given string can be retrieved in the catch statement by using toString.
Example:
class myException : Exception
{
this(char[] msg)
{
super(msg);
}
}
try {
throw new myException("Hello World!");
} catch (myException ex) {
writefln("My exception was thrown:");
writefln(ex.toString());
}
|
Output:
My exception was thrown:
Hello World! |
See also:
Try statement,
Classes,
Error Handling
|