33

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?

identigral
  • 7,543
  • 29
  • 32
  • 42
Adrian Larson
  • 149,971
  • 38
  • 239
  • 420

1 Answers1

66

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.

Matt and Neil
  • 32,894
  • 7
  • 105
  • 186
Adrian Larson
  • 149,971
  • 38
  • 239
  • 420
  • 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 '16 at 01:48
  • 3
    Such a dirty question and answer combo, but i love it. Excellent discovery that warrants the combo. – cricketlang Feb 23 '16 at 03: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 '16 at 21:11
  • @ktbiz The empty constructor worked when I originally wrote this question. That sounds like a good new question for you to ask if that is no longer the case! – Adrian Larson Sep 28 '16 at 21:18
  • 6
    Apparently InvalidParameterValueException has a 2 string constructor. e.g. new InvalidParameterValueException('foo', 'bar');. – ktbiz Oct 13 '16 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 07 '17 at 22:11
  • Here are a couple of uncatchable system exceptions that you can also construct and throw: FinalException, and AssertException (which can officially be thrown by calling any flavor of System.assert(...)). – Tim Lewis May 16 '19 at 14:21