Catch System.AssertException

Is there a way to catch System.AssertException?

For example, some class is known to throw assertions in certain cases and it’s ok to deal with that on some upper level. But unfortunately straightforward try-catch doesn’t work for assert. I expect that this code will not throw an exception but it does:

    System.assert(false, 'fail');
}catch(System.AssertException asEx){

So, is there a way to implement ability of catching asserts?


Short answer no.

From the Apex Developers Guide:

Asserts that condition is true. If it is not, a fatal error is
returned that causes code execution to halt. The returned error
optionally contains the custom message specified in the last argument.

You can’t catch an assertion failure using a try/catch block even
though it is logged as an exception.

You’ll need to convert your asserts to exceptions if you want to handle them either at the point that they are thrown, or in any calling code.

Source : Link , Question Author : Vasily Liaskovsky , Answer Author : Bob Buzzard

Leave a Comment