• Lessons Home
Topics
Community
  1. Lessons
  2. Object-Oriented Apex
  3. Exceptions
  4. Built-in Exception Types

      Built-in Exception Types

      Built-in Exception Types

      A catch (Exception e) block catches every kind of error Apex knows about, but most of the time only one kind can actually happen at the place you are guarding. Catching the specific type makes the code say exactly what went wrong, and it stops a different, unexpected failure from being swallowed by the wrong fallback.

      Common Built-in Types

      A few built-in types come up over and over:

      • NullPointerException - you dereferenced a variable that held null
      • ListException - the index you used is past the end of the list, or negative
      • DmlException - a insert, update, or delete failed for one or more records
      • QueryException - a SOQL query found no rows when one was required, or the query itself was malformed
      • MathException - a calculation went wrong, like dividing an Integer by zero
      • TypeException - a cast or valueOf call did not match the destination type

      All of them extend Exception, so a more specific catch always runs before a generic one in the same try:

      try {
          Account a = [SELECT Id FROM Account WHERE Id = :someId LIMIT 1];
          update a;
      } catch (QueryException e) {
          System.debug('No account matched that id');
      } catch (DmlException e) {
          System.debug('Update failed: ' + e.getMessage());
      }
      

      Inspecting the Exception at Runtime

      Even when you catch a specific type, the variable still has useful methods on it. e.getTypeName() returns the name of the actual exception class that was thrown, which is handy when a catch (Exception e) covers several possible failures and you want to react differently to each:

      try {
          doSomethingRisky();
      } catch (Exception e) {
          if (e.getTypeName() == 'ListException') {
              return Collections.emptyList();
          }
          throw e;
      }
      

      Why This Matters

      Catching the specific type you expect is clearer to read, and it lets unexpected exceptions keep propagating instead of being silently turned into a wrong answer. The built-in type names are also the names you will see in stack traces and debug logs, so recognising them shortens the time between a bug report and a fix.

      Apex Code Editor
      Sign in to Submit

      Welcome to Lightning Challenge!

      How It Works

      • • Write your solution in the code editor
      • • Connect your Salesforce org to test
      • • Submit to check if your solution passes
      • • Use hints if you get stuck

      Note

      Complete this lesson challenge to earn points and track your progress. The code editor allows you to implement your solution, and the tests will verify if your code meets the requirements.

      Wally Assistant

      Wally can't hear you

      Please sign in to access the AI Assistant

      Sign In