SalesforceBlue

Feel the rhythm of Salesforce

Apex

Apex Exception Handling Simplified

The term “exception” means “exceptional condition” and is an occurrence that alters the normal program flow.
When an exceptional event occurs in Apex, an exception is said to be “thrown.” The code that’s responsible for doing something about the exception is called an “exception handler,” and it “catches” the thrown exception.

Apex exceptions can be categorized into the following:

  1. Built-In Exceptions: These are standard exceptions given by Salesforce. For Examples – DMLException, ListException, MathException, NoAcessException, NoDataFoundException, NullPointerException, QueryException, QueryException, TypeException, etc.

  2. Custom Exceptions: These are custom exceptions that we can create. We covered custom exceptions and how to create them below.
Apex try-catch block:

An exception can occur in the try block and is handled in the respective catch block.

try {
    // try block 
} catch(Exception e) {
    // catch block
}
Apex try-catch-finally block:

The finally block always executes whether an exception occurs or not.

try {
    // try block 
} catch(Exception e) {
    // catch block
} finally {
    // finally block
}
Apex try-finally block:

The finally block can be added without a catch block as well.

Below is a valid code block.

try {
    // try block 
} finally {
    // finally block
}
Exception Methods:

The Exception class contains standard methods for interrogating exceptions. Let’s see the frequently used methods one by one below.

getCause()

It returns the cause of an exception as an exception object.

getLineNumber()

It returns the line number from where the exception was thrown.

getMessage()

It returns the error message that displays for the user.

getStackTraceString()

It returns the stack trace as a String.

getTypeName()

It returns the type of Exception such as NullPointerException, DMLException, etc.

The below example covers all the above methods:

public class BlueUtility {
    public static void divideByZero() {
        try {            
            Integer a = 10/0; // an exception will be thrown here
        } catch(Exception e) {
            System.debug(e.getCause());
            System.debug(e.getLineNumber());
            System.debug(e.getMessage());
            System.debug(e.getStackTraceString());
            System.debug(e.getTypeName());
        }
    }
}
Create Custom Apex Exception:

We can create our own custom exception class and can throw this on a specific condition. This exception class must end with the string “exception” such as MyPageLoadException.

public class MyPageLoadException extends Exception {

}
Throwing Custom Apex Exception:

We can throw a custom apex exception using the throw keyword.

public class BlueUtility {
    public static void pageLoad() {
        try {
            // throw custom exception on certain pre conditions
            throw new MyPageLoadException();
        } catch(Exception e) {
            System.debug(e.getLineNumber());
        }
    }
}

Custom exception supports four types of implicit constructors. They are as follows:

with no arguments:

new MyPageLoadException();

With a single String argument that specifies the error message:

new MyPageLoadException('Page has no data');

With a single Exception argument that specifies the cause and that displays in any stack trace:

new MyPageLoadException(e);

With both a String error message and a chained exception cause that displays in any stack trace:

new MyPageLoadException('Page has no data', e);

Thank you for visiting SalesforceBlue.com
If you have any queries feel free to write down a comment below 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *