Error

The base class of all unrecoverable runtime errors.

This represents the category of Throwable objects that are not safe to catch and handle. In principle, one should not catch Error objects, as they represent unrecoverable runtime errors. Certain runtime guarantees may fail to hold when these errors are thrown, making it unsafe to continue execution after catching them.

Constructors

this
this(string msg, Throwable nextInChain = null)

Creates a new instance of Error. The nextInChain parameter is used internally and should always be null when passed by user code. This constructor does not automatically throw the newly-created Error; the throw statement should be used for that purpose.

Members

Variables

bypassedException
Throwable bypassedException;

The first Exception which was bypassed when this Error was thrown, or null if no Exceptions were pending.

Inherited Members

From Throwable

file
string file;

The file name of the D source code corresponding with where the error was thrown from.

line
size_t line;

The line number of the D source code corresponding with where the error was thrown from.

info
TraceInfo info;

The stack trace of where the error happened. This is an opaque object that can either be converted to string, or iterated over with foreach to extract the items in the stack trace (as strings).

next
inout(Throwable) next()
next
void next(Throwable tail)

Replace next in chain with tail. Use chainTogether instead if at all possible.

refcount
uint refcount()
opApply
int opApply(scope int delegate(Throwable) dg)

Loop over the chain of Throwables.

chainTogether
Throwable chainTogether(return scope Throwable e1, return scope Throwable e2)

Append e2 to chain of exceptions that starts with e1.

toString
string toString()

Overrides Object.toString and returns the error message. Internally this forwards to the toString overload that takes a sink delegate.

toString
void toString(scope void delegate(in char[]) sink)

The Throwable hierarchy uses a toString overload that takes a _sink delegate to avoid GC allocations, which cannot be performed in certain error situations. Override this toString method to customize the error message.

message
const(char)[] message()

Get the message describing the error. Base behavior is to return the Throwable.msg field. Override to return some other error message.

Examples

1 bool gotCaught;
2 try
3 {
4     throw new Error("msg");
5 }
6 catch (Error e)
7 {
8     gotCaught = true;
9     assert(e.msg == "msg");
10 }
11 assert(gotCaught);
Suggestion Box / Bug Report