std.exception
This module defines functions related to exceptions and general error handling. It also defines functions intended to aid in unit testing.
Copyright
Types 3
Enforces that the given value is true, throwing an ErrnoException if it is not.
Parameters
value | The value to test. |
msg | The message to include in the ErrnoException if it is thrown. |
Returns
value, if cast(bool) value is true. Otherwise,
new ErrnoException(msg) is thrown. It is assumed that the last
operation set errno to an error code corresponding with the failed condition.
Thrown if errors that set errno occur.
private uint _errnoprivate string _errnoMsgstring errnoMsg() @property nothrow pure scope @nogc @safeLocalized error message generated through strerror_r or strerror.This enum is used to select the primitives of the range to handle by the
enum can be OR'd to
select multiple primitives to be handled.
RangePrimitive.access is a shortcut for the access primitives; front, back and opIndex.
RangePrimitive.pop is a shortcut for the mutating primitives; popFront and popBack.
Functions 22
auto assertNotThrown(T : Throwable = Exception, E)(lazy E expression,
string msg = null,
string file = __FILE__,
size_t line = __LINE__)Asserts that the given expression does not throw the given type of `Throwable`. If a `Throwable` of the given type is thrown, it is caught and does not escape assertNotThrown. Rather, an `AssertErr...void assertThrown(T : Throwable = Exception, E)(lazy E expression,
string msg = null,
string file = __FILE__,
size_t line = __LINE__)Asserts that the given expression throws the given type of `Throwable`. The `Throwable` is caught and does not escape assertThrown. However, any other `Throwable`s will escape, and if no `Throwable...T enforce(T, Dg, string file = __FILE__, size_t line = __LINE__)(T value, scope Dg dg) if (isSomeFunction!Dg && is(typeof( dg() )) &&
is(typeof({ if (!value) { } })))dittonoreturn bailOut(E : Throwable = Exception)(string file, size_t line, scope const(char)[] msg)T collectException(T = Exception, E)(lazy E expression, ref E result)Catches and returns the exception thrown from the given expression. If no exception is thrown, then null is returned and `result` is set to the result of the expression.T collectException(T : Throwable = Exception, E)(lazy E expression)Catches and returns the exception thrown from the given expression. If no exception is thrown, then null is returned. `E` can be `void`.string collectExceptionMsg(T = Exception, E)(lazy E expression)Catches the exception thrown from the given expression and returns the msg property of that exception. If no exception is thrown, then null is returned. `E` can be `void`.immutable(T)[] assumeUnique(T)(T[] array) pure nothrowCasts a mutable array to an immutable array in an idiomatic manner. Technically, `assumeUnique` just inserts a cast, but its name documents assumptions on the part of the caller. `assumeUnique(arr)...T assumeWontThrow(T)(lazy T expr,
string msg = null,
string file = __FILE__,
size_t line = __LINE__) nothrowWraps a possibly-throwing expression in a `nothrow` wrapper so that it can be called by a `nothrow` function.bool doesPointTo(S, T, Tdummy = void)(auto ref const S source, ref const T target) if (__traits(isRef, source) || isDynamicArray!S ||
is(S == U *, U) || is(S == class)) @nogc @trusted pure nothrowChecks whether a given source object contains pointers or references to a given target object.bool doesPointTo(S, T)(auto ref const shared S source, ref const shared T target) @trusted pure nothrowdittobool mayPointTo(S, T, Tdummy = void)(auto ref const S source, ref const T target) if (__traits(isRef, source) || isDynamicArray!S ||
is(S == U *, U) || is(S == class)) @trusted pure nothrowdittobool mayPointTo(S, T)(auto ref const shared S source, ref const shared T target) @trusted pure nothrowdittoCommonType!(T1, T2) ifThrown(E : Throwable = Exception, T1, T2)(lazy scope T1 expression, lazy scope T2 errorHandler)ML-style functional exception handling. Runs the supplied expression and returns its result. If the expression throws a `Throwable`, runs the supplied error handler instead and return its result. T...CommonType!(T1, T2) ifThrown(E : Throwable, T1, T2)(lazy scope T1 expression, scope T2 delegate(E) errorHandler)dittoCommonType!(T1, T2) ifThrown(T1, T2)(lazy scope T1 expression, scope T2 delegate(Exception) errorHandler)dittoauto handle(E : Throwable, RangePrimitive primitivesToHandle, alias handler, Range)(Range input) if (isInputRange!Range)Handle exceptions thrown from range primitives.Variables 1
emptyExceptionMsg = "<Empty Exception Message>"Value that collectExceptionMsg returns when it catches an exception with an empty exception message.
Templates 1
if (is(typeof(new E("", string.init, size_t.init)) : Throwable) ||
is(typeof(new E(string.init, size_t.init)) : Throwable))Enforces that the given value is true. If the given value is false, an exception is thrown. The
msg- error message as astringdg- custom delegate that return a string and is only called if an exception occurredex- custom exception to be thrown. It islazyand is only created if an exception occurred
Parameters
value | The value to test. |
E | Exception type to throw if the value evaluates to false. |
msg | The error message to put in the exception if it is thrown. |
dg | The delegate to be called if the value evaluates to false. |
ex | The exception to throw if the value evaluates to false. |
file | The source file of the caller. |
line | The line number of the caller. |
Returns
value, if cast(bool) value is true. Otherwise,
depending on the chosen overload, new Exception(msg), dg() or ex is thrown.
Note
enforce is used to throw exceptions and is therefore intended to
aid in error handling. It is not intended for verifying the logic of your program - that is what assert is for.
Do not use enforce inside of contracts (i.e. inside of in and out blocks and invariants), because contracts are compiled out when compiling with -release.
If a delegate is passed, the safety and purity of this function are inferred from Dg's safety and purity.