std.exception.enforce
- multiple declarations
- Function enforce
- Function enforce
- Function enforce
- Function enforce
Function enforce
If !!
is true, value
is returned. Otherwise,
value
new Exception(
is thrown.
msg
)
Prototype
T enforce(E, T)( T value, const(char)[] msg = null, string file = __FILE__, size_t line = __LINE__ ) if (is(typeof(() { if (!value) { } } )));
Note
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 enforce
assert
is for. Also, do not use
inside of contracts (i.e. inside of enforce
in
and out
blocks and invariant
s), because they will be compiled out when
compiling with -release. Use assert
in contracts.
Example
auto f = enforce(fopen("data.txt")); auto line = readln(f); enforce(line.length, "Expected a non-empty line.");
Function enforce
Deprecated. If passing the file or line number explicitly, please use
the overload of enforce
which takes them as function arguments. Taking
them as template arguments causes unnecessary template bloat. This
overload will be removed in June 2015.
Prototype
T enforce(T, string file, ulong line)( T value, const(char)[] msg = null ) if (is(typeof(() { if (!value) { } } )));
Function enforce
If !!
is true, value
is returned. Otherwise, the given
delegate is called.
value
The whole safety and purity are inferred from Dg
's safety and purity.
Prototype
T enforce(T, Dg, string file, ulong line)( T value, Dg dg ) if (isSomeFunction!Dg && is(typeof(dg())) && is(typeof(() { if (!value) { } } )));
Function enforce
If !!
is true, value
is returned. Otherwise, value
is thrown.
ex
Prototype
T enforce(T)( T value, Throwable ex );
Example
auto f = enforce(fopen("data.txt")); auto line = readln(f); enforce(line.length, new IOException); // expect a non-empty line
Authors
Andrei Alexandrescu and Jonathan M Davis