The type of exception to catch.
The expression which may throw an exception.
The result of the expression if no exception is thrown.
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)); }
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.
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.