In Creating Custom Exceptions, the documentation still claims:

Since you can’t throw built-in Apex exceptions but can only catch them, you can create custom exceptions to throw in your methods.

Is it still true that built-in Exception types cannot be thrown?

1 Answer 1


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:

    CalloutException e = new CalloutException();
    e.setMessage('This is a constructed exception!');
    throw e;
catch (Exception pokemon)
{ // gotta catch em all!

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.

  • 2
    +1 for "well way cool" - I recall elaborate workarounds I made to simulate CalloutException to test HttpRequest timeout/retry logic -- did you post feedback to the doc page?
    – cropredy
    Feb 23, 2016 at 1:48
  • 3
    Such a dirty question and answer combo, but i love it. Excellent discovery that warrants the combo. Feb 23, 2016 at 3:13
  • What is the constructor for InvalidParameterValueException? Trying to instantiate it with no-arg or (String) constructor causes a compile time error.
    – ktbiz
    Sep 28, 2016 at 21:11
  • 5
    Apparently InvalidParameterValueException has a 2 string constructor. e.g. new InvalidParameterValueException('foo', 'bar');.
    – ktbiz
    Oct 13, 2016 at 20:20
  • 1
    This is awesome because this lets me create a contrived method for getting around the STUPID limit on the length of log files. Bless you!
    – noctufaber
    Feb 7, 2017 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.