std.exception.collectException
- multiple declarations
Function collectException
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, E)
(
lazy E expression,
ref E result
);
Note that while collectException
can be used to collect any
Throwable
and not just Exception
s, it is generally ill-advised to
catch anything that is neither an Exception
nor a type derived from
Exception
. So, do not use collectException
to collect
non-Exception
s unless you're sure that that's what you really want to
do.
Parameters
Name | Description |
---|---|
T | The type of exception to catch. |
expression | The expression which may throw an exception. |
result | The result of the expression if no exception is thrown. |
Example
int b;
int foo() { throw new Exception("blah"); }
assert(collectException(foo(), b));
int[] a = new int[3];
import core .exception : RangeError;
assert(collectException!RangeError(a[4], b));
Function collectException
Catches and returns the exception thrown from the given expression.
If no exception is thrown, then null is returned. E
can be
void
.
T collectException(T, E)
(
lazy E expression
);
Note that while collectException
can be used to collect any
Throwable
and not just Exception
s, it is generally ill-advised to
catch anything that is neither an Exception
nor a type derived from
Exception
. So, do not use collectException
to collect
non-Exception
s unless you're sure that that's what you really want to
do.
Parameters
Name | Description |
---|---|
T | The type of exception to catch. |
expression | The expression which may throw an exception. |
Example
int foo() { throw new Exception("blah"); }
writeln(collectException(foo()) .msg); // "blah"