0% found this document useful (0 votes)
20 views

Exceptions Examples

The document provides examples of using try-catch blocks to handle exceptions in Apex. It demonstrates how to catch specific exception types like DMLException and SObjectException. It also shows how to define a custom exception by extending the Exception class and how to throw custom exceptions. Finally, it displays the methods to access details of the caught exception like getCause(), getMessage(), getStackTraceString() etc.

Uploaded by

Sriram dharahas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Exceptions Examples

The document provides examples of using try-catch blocks to handle exceptions in Apex. It demonstrates how to catch specific exception types like DMLException and SObjectException. It also shows how to define a custom exception by extending the Exception class and how to throw custom exceptions. Finally, it displays the methods to access details of the caught exception like getCause(), getMessage(), getStackTraceString() etc.

Uploaded by

Sriram dharahas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Exception Methods

try{
Lead l = [Select Name from Lead limit 1];
string des = l.description; // throws an exception
}catch(Exception e){
system.debug('Cause: '+e.getCause());
system.debug('Line Number: '+e.getLineNumber());
system.debug('Message: '+e.getMessage());
system.debug('stack Trace: '+e.getStackTraceString());
system.debug('Exception Type: '+e.getTypeName());
}

example 2 :
try{
Account acc = [Select Id,Name from Account limit 1];
String s = acc.site;

}catch(DMLException e){

system.debug('DML exception occured: '+e.getMessage());

}catch(SObjectException e){

system.debug('SObject exception occured: '+e.getMessage());

}catch(Exception e){
system.debug('Exception Occured: '+e.getMessage());

} finally{
system.debug('Cleaning up data in finally');
}

debug statements :
DEBUG|SObject exception occured: SObject row was retrieved via SOQL without
querying the requested field: Account.Site
DEBUG|Cleaning up data in finally

Example 3:
try{
Account acc = [Select Id,Name from Account limit 10];
String s = acc.Name;

}catch(DMLException e){

system.debug('DML exception occured: '+e.getMessage());

}catch(SObjectException e){

system.debug('SObject exception occured: '+e.getMessage());

}catch(Exception e){
system.debug('Exception Occured: '+e.getMessage());

} finally{
system.debug('Cleaning up data in finally');
}
Debug Statments:
Exception Occured: List has more than 1 row for assignment to SObject
DEBUG|Cleaning up data in finally

Example 4:
try{
Account acc = [Select Id,Name from Account limit 1];
String s = acc.Site;

}catch(Exception e){
system.debug('Exception Occured: '+e.getMessage());

}catch(DMLException e){

system.debug('DML exception occured: '+e.getMessage());

}catch(SObjectException e){

system.debug('SObject exception occured: '+e.getMessage());

} finally{
system.debug('Cleaning up data in finally');
}

Error : Exception type already caught

Example 5 : Custom exceptions

public class acctException extends Exception {

public class AccountUtility {


public static void mainProcecessing(){
try{
insertAccount();
}catch(acctException e){
system.debug('Cause: '+e.getCause());
system.debug('Line Number: '+e.getLineNumber());
system.debug('Message: '+e.getMessage());
system.debug('stack Trace: '+e.getStackTraceString());
system.debug('Exception Type: '+e.getTypeName());
}
}
public static void insertAccount(){
try{
Account a = new Account();
Insert a;
}catch(DMLException de){
throw new acctException('Account cannot be created bacause required
fields are missing',de);
}
}
}

You might also like