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 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.

  1. T collectException(E expression, E result)
  2. T collectException(E expression)
    T
    collectException
    (
    T : Throwable = Exception
    E
    )
    (
    lazy E expression
    )

Parameters

T

The type of exception to catch.

expression E

The expression which may throw an exception.

Examples

int foo() { throw new Exception("blah"); }
assert(collectException(foo()).msg == "blah");

Meta