As far as I know, @mattandneil was the first to discover this change in this answer to a question by @DanielBallinger.
Despite the documentation's claims to the contrary, the compiler now allows built-in Exception types to be constructed. I confirmed through Execute Anonymous with the following snippet:
try
{
CalloutException e = new CalloutException();
e.setMessage('This is a constructed exception!');
throw e;
}
catch (Exception pokemon)
{ // gotta catch em all!
system.debug(pokemon);
}
I went through the whole list and what do you know? The following types can all be constructed and thrown:
- AsyncException
- CalloutException
- DmlException
- EmailException
- ExternalObjectException
- InvalidParameterValueException
- LimitException (though it still can't be caught)
- JSONException
- ListException
- MathException
- NoAccessException
- NoDataFoundException
- NoSuchElementException
- NullPointerException
- QueryException
- RequiredFeatureMissingException
- SearchException
- SecurityException
- SerializationException
- SObjectException
- StringException
- TypeException
- VisualforceException
- XmlException
I must admit it feels weird that you can throw new LimitException(). I submitted Feedback on the linked documentation requesting that they update their content.
new InvalidParameterValueException('foo', 'bar');. – ktbiz Oct 13 '16 at 20:20FinalException, andAssertException(which can officially be thrown by calling any flavor ofSystem.assert(...)). – Tim Lewis May 16 '19 at 14:21