Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Exceptions are statements that are used to note down errors or any other events that disrupt the normal
execution of Apex code.
Following incidents occur when there are exceptions and they are not properly handled:
Any DML operations that were performed as a part of the transaction are rolled back.
Also, Apex sends out an unhandled exception email to the user who last modified
the class or trigger due to which the error has occurred.
Exception handling is the process of responding to exceptions that appear in the code during its run time.
There are many ways to contain an exception in the Apex code such as ensuring every scenario, positive or
negative, is handled.
Exception Handling
Exception!
Throughout this lesson, where “exception” has been used to mean errors, it has been represented in lowercase.
Where “Exception” has been used to mean built-in class, it has been represented in title case.
©Simplilearn. All rights reserved 6
Exception Statements
Understand the different keywords and statements that are used to catch
and handle exceptions in Apex
There are statements that help in catching and handling exceptions in Apex code. Let’s discuss them
individually. They are:
02 Try-catch-finally statements.
A throw statement is used to signal that an exception has occurred. The basic syntax of throw statement is
shown here:
throw exceptionObject;
Try, catch, and finally are the Exception statements that can be used together to capture an exception
and handle it gracefully.
No Exception
Exception handler
handler
… Try {
…. Exceptions …
Code.. Code..
… ….
…. }
Catch block
Error!
code_block
}
catch (exceptionType variableName) {
code_block
catch (Exception e) {
Catch block is optional and comes immediately after the try
// Optional additional catch statement for other exception types. block and handles the exception.
// Note that the general exception type, 'Exception',
code_block
finally {
// Finally block.
a. try
b. catch
c. finally
a. try
b. catch
c. finally
The finally block of code should be used to clear out the variables in code.
System-defined exception or built-in exceptions are Exception classes provided by Salesforce out of the box.
Types of
Exceptions
Custom or
system-defined
user-defined
NullPointer Generic
DMLException ListException QueryException
Exception Exception Type
DML Exceptions are exceptions that occur whenever a DML operation fails. This may happen due to many reasons, the
most common one is inserting a record without a required field.
NullPointer Generic
DMLException ListException QueryException
Exception Exception Type
try {
insert pos;
} catch(DmlException e) {
ListException catches any type of run time error with a list. This list can be of any data type such as integer, string, or
sObject.
NullPointer Generic
DMLException ListException QueryException
Exception Exception Type
try {
stringList.add('John');
} catch(ListException le) {
NullPointer Exception catches exceptions that occur when we try to reference a null variable. Use this exception
whenever you are referencing a variable that might turn out to be null.
NullPointer Generic
DMLException ListException QueryException
Exception Exception Type
try {
String stringVariable;
} catch(NullPointerException npe) {
QueryException catches any run time errors with SOQL queries. QueryException occurs when there is a problem in
SOQL queries such as assigning a query that returns no records or more than one record to a single sObject variable.
NullPointer Generic
DMLException ListException QueryException
Exception Exception Type
try {
// This statement doesn't cause an exception,
// if we don't have a
// The list will just be empty.
List<Position__c> positionList = [SELECT Name FROM Position__c WHERE Name='Salesforce Developer'];
// positionList.size() is 0
System.debug(positionList.size());
This exception type can catch any type of exception; that’s why it’s called generic Exception type. This is used when you
are not sure which exception type to use and what exception might occur in the code.
NullPointer Generic
DMLException ListException QueryException
Exception Exception Type
try {
// This statement doesn't cause an exception,
// if we don't have a
// The list will just be empty.
List<Position__c> positionList = [SELECT Name FROM Position__c WHERE Name='Salesforce Developer'];
// positionList.size() is 0
System.debug(positionList.size());
All Exception types are Exception classes and these classes have methods that can provide a lot of information about
the exception and error that occurred.
insert pos;
user.
} catch(Exception e) { getCause: Returns the cause of the exception as an exception
System.debug('Exception type caught: ' + e.getTypeName());
object.
System.debug('Message: ' + e.getMessage());
getLineNumber: Returns the line number from where the
System.debug('Cause: ' + e.getCause()); // returns null
You can use several catch blocks—a catch block for each exception type and a final catch block that
catches the generic Exception type.
try {
insert pos;
} catch(DmlException e) {
} catch(QueryException e) {
} catch(Exception e) {
With a single String argument that specifies the error message: new MyException('This is bad');
1 Exception { } Which statement would you use to throw the exception? Select any two.
a. throw MyCustomException();
1 Exception { } Which statement would you use to throw the exception? Select any two.
a. throw MyCustomException();
Use throw new MyCustomException('Problem found'); and throw new MyCustomException(); to throw the
exception.
a. stringList[0]
b. stringList[1]
c. stringList[2]
d. stringList[3]
a. stringList[0]
b. stringList[1]
c. stringList[2]
d. stringList[3]
Since there are only two items in the list, here you are trying to access the item at 3rd index; this will throw
an error.
Since we are fetching records in a list, List<Position__c> posList = [select id from Position__c limit 1];
will not throw an exception.
We declare and define custom exception classes because custom exceptions enable you to specify detailed
error messages.
5 exception to be caught?
a. Yes
b. No
5 exception to be caught?
a. Yes
b. No
If a catch block on the 1st or 2nd level is relevant to the exception thrown, then other catch blocks below it
are not executed.