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:
try{ System.assert(false, 'fail'); }catch(System.AssertException asEx){ System.debug('success'); }
So, is there a way to implement ability of catching asserts?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0