Master the concepts of Salesforce in just 45 days Click here to know more.
8
10
58
27
Premium Salesforce Weekend Batch Start From Tomorrow.
You can create your own top level Exception classes that can have their own member variables, methods, constructors, implement interfaces, and so on.
Ex: public class MyCustomException extends Exception {}
Ex: public class MyOtherException extends MyCustomException {}
Ex: new MyException();
Ex: new MyException(‘This is bad’);
Ex: new MyException(e);
Ex: new MyException(‘This is bad’, e);
Example:
public class PositionException extends Exception {}
public class PositionUtility {
public static void mainProcessing() {
try {
insertPositionMethod();
} catch(PositionException pe) {
System.debug(‘Message: ‘ + pe.getMessage());
System.debug(‘Cause: ‘ + pe.getCause());
System.debug(‘Line number: ‘ + pe.getLineNumber());
System.debug(‘Stack trace: ‘ + pe.getStackTraceString());
}
public static void insertPositionMethod() {
// Insert Position record without required fields
Position__c pos = new Position__c();
insert pos;
} catch(DmlException e) {
// Since Position record is being insert
// without required fields, DMLException occurs
throw new PositionException(
‘Position record could not be inserted.’, e);
There are different types of exception statements in apex:
A throw statement is used to generate an exception or 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.
Try: A try block encloses the code where exception can occur.
Catch: Catch block is optional and comes immediately after the try block and can handle a particular type of exception. A single try statement can have zero or more associated catch statements where each catch statement must have a unique exception type.
Note: Once a particular exception type is caught in one catch block, the remaining catch blocks (if any) are not executed.
Finally: Finally block is mandatorily executed at the end whether the exception occured in the try block or not.
It is not compulsory for every try block to have finally associated with it and also there can be at max only 1 finally block that can be associated to 1 try block.
Finally block is generally used to cleanup code or for freeing up resources.
Note: With every try block there should be at least 1 catch or 1 finally block associated.
Let’s look at the syntax of try, catch, and finally statements:
try
{
// Try block
catch (exceptionType variableName)
// Initial catch block.
// At least 1 catch block or finally block must be present.
catch (Exception e)
// Optional additional catch statement for other exception types.
// Note that the general exception type, ‘Exception’ must be the last catch block when it is used.
finally
// Finally block.
// this code block is always executed
There are some special types of built-in exceptions that can’t be caught. Those exceptions are associated with critical situations in the Lightning Platform. These situations require the abortion of code execution and don’t allow for execution to resume through exception handling.
One such exception is the limit exception (System.LimitException) that the runtime throws if a governor limit has been exceeded, such as when the maximum number of SOQL queries issued has been exceeded.
Other examples are exceptions thrown when assertion statements fail (through System.assert methods) or license exceptions.
Note: When exceptions are uncatchable, catch blocks, as well as finally blocks if any, aren’t executed.
< < Previous
Next > >
The batch is going to start shortly. Fill the form given below to Register yourself now.
Fill out the form and get consulted by our Salesforce experts.
Fill out the form to figure out to detemine the perfect Salesforce profile for yourself.
Fill the form below to get a demo of this course.
Fill out the form to get your Premium Salesforce Development Course
Online Salesforce Development Course is soon going to be launched. Please fill the form and we will notify you about the course.