Exceptions Examples
Exceptions Examples
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){
}catch(SObjectException e){
}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){
}catch(SObjectException e){
}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){
}catch(SObjectException e){
} finally{
system.debug('Cleaning up data in finally');
}