View source code
Display the source code in std/exception.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

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 Exceptions, 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-Exceptions unless you're sure that that's what you really want to do.

Parameters

NameDescription
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));

version (D_NoBoundsChecks) {}
else
{
    // check for out of bounds error
    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 Exceptions, 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-Exceptions unless you're sure that that's what you really want to do.

Parameters

NameDescription
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"

Authors

Andrei Alexandrescu and Jonathan M Davis

License

Boost License 1.0