std.exception.collect_exception
- multiple declarations
- Function collectException
- Function collectException
Function collectException
Catches and returns the exception thrown from the given expression
.
If no exception is thrown, then null is returned and
is
set to the result
result
of the expression
.
Note that while
can be used to collect any
collectException
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
to collect
non-collectException
Exception
s unless you're sure that that's what you really want to
do.
Prototype
T collectException(T, E)( E expression, E result );
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
.
Note that while
can be used to collect any
collectException
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
to collect
non-collectException
Exception
s unless you're sure that that's what you really want to
do.
Prototype
T collectException(T, E)( E expression );
Parameters
Name | Description |
---|---|
T | The type of exception to catch. |
expression | The expression which may throw an exception. |
Authors
Andrei Alexandrescu and Jonathan M Davis