Exception

The base class of all errors that are safe to catch and handle.

In principle, only thrown objects derived from this class are safe to catch inside a catch block. Thrown objects not derived from Exception represent runtime errors that should not be caught, as certain runtime guarantees may not hold, making it unsafe to continue program execution.

Constructors

this
this(string msg, string file, size_t line, Throwable nextInChain)

Creates a new instance of Exception. 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 Exception; the throw statement should be used for that purpose.

Inherited Members

From Throwable

msg
string msg;

A message describing the error.

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 [@property getter]
next
Throwable next [@property setter]

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

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

Loop over the chain of Throwables.

chainTogether
Throwable chainTogether(Throwable e1, 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(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.

Examples

bool gotCaught;
try
{
    throw new Exception("msg");
}
catch (Exception e)
{
    gotCaught = true;
    assert(e.msg == "msg");
}
assert(gotCaught);

Meta