std.exception.enforce
- multiple declarations
- Function enforce
- Function enforce
- Function enforce
Function enforce
Enforces that the given value
is true.
Prototype
T enforce(E, T)( T value, const(char)[] msg = null, string file = __FILE__, size_t line = __LINE__ ) if (is(typeof(() { if (!value) { } } )));
Parameters
Name | Description |
---|---|
value | The value to test. |
E | Exception type to throw if the value evalues to false. |
msg | The error message to put in the exception if it is thrown. |
file | The source file of the caller. |
line | The line number of the caller. |
Returns
, if value
!
is false. Otherwise,
value
new Exception(
is thrown.
msg
)
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
Enforces that the given value
is true.
Prototype
T enforce(T, Dg, string file, ulong line)( T value, Dg dg ) if (isSomeFunction!Dg && is(typeof(dg())) && is(typeof(() { if (!value) { } } )));
Parameters
Name | Description |
---|---|
value | The value to test. |
dg | The delegate to be called if the value evaluates to false. |
file | The source file of the caller. |
line | The line number of the caller. |
Returns
if value
!
is false. Otherwise, the given delegate
is called.
value
The safety and purity of this function are inferred from Dg
's safety
and purity.
Function enforce
Enforces that the given value
is true.
Prototype
T enforce(T)( T value, Throwable ex );
Parameters
Name | Description |
---|---|
value | The value to test. |
ex | The exception to throw if the value evaluates to false. |
Returns
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