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.
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.
Ex: try
{
Position__c pos = new Position__c();
insert pos;
}
catch(DmlException e)
System.debug(‘The following exception has occurred: ‘ + e.getMessage());
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.
List<String> stringList = new List<String>();
stringList.add(‘Bhavna’);
// This list contains only one element,
// but we will attempt to access the second element
// from this zero-based list.
String str1 = stringList[0]; //this will execute fine
String str2 = stringList[1]; // Causes a ListException
catch(ListException le)
System.debug(‘The following exception has occurred: ‘ + le.getMessage());
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.
String stringVariable;
Boolean boolVariable = stringVariable.contains(‘John’);
// Causes a NullPointerException
catch(NullPointerException npe)
System.debug(‘The following exception has occurred: ‘ + npe.getMessage());
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.
Ex: try {
// This statement doesn’t cause an exception,
// if we don’t have a problem in SOQL Queries.
// 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());
// However, this statement causes a QueryException because
// we’re assiging the query result to a Position sObject varaible
// but no Position record is found
Position__c pos = [SELECT Name FROM Position__c WHERE Name=’Salesforce Developer’ LIMIT 1];
catch(QueryException ge) {
System.debug(‘The following exception has occurred: ‘ + ge.getMessage());
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.
} catch(Exception ex) {
System.debug(‘The following exception has occurred: ‘ + ex.getMessage());
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.
Position__c pos = new Position();
// Causes an QueryException because
// we are inserting record without required fields
catch(Exception e)
System.debug(‘Exception type caught: ‘ + e.getTypeName());
System.debug(‘Message: ‘ + e.getMessage());
System.debug(‘Cause: ‘ + e.getCause()); // returns null
System.debug(‘Line number: ‘ + e.getLineNumber());
USER_DEBUG|[6]|DEBUG|Exception type caught: System.QueryException
USER_DEBUG|[7]|DEBUG|Message: List has no rows for assignment to SObject
USER_DEBUG|[8]|DEBUG|Cause: null
USER_DEBUG|[9]|DEBUG|Line number: 2
USER_DEBUG|[10]|DEBUG|Stack trace: AnonymousBlock: line 2, column 1
< < 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.