//QueryException
Try{
Account a = [select id, name from Account where name = 'test'];//list has no rows
}
catch( QueryException e ) {
System.debug( e.getMessage() );
}
//SObjectException
try {
Merchandise__c m = [SELECT Name FROM Merchandise__c LIMIT 1];
// Causes an SObjectException because we didn't retrieve
// the Total_Inventory__c field.
Double inventory = m.Total_Inventory__c;
} catch(SObjectException e) {
System.debug('The following exception has occurred: ' + e.getMessage());
}
In your test class, you need to make some Error like QueryException, SObjectException that way the try will send to Catch method, then your code will be covered.
Hi Gokula thanks for reply.. since i dont have any testclass knowledge .. you you provide code snippet for QueryException, SObjectException and dml exception
//QueryException
Try{
Account a = [select id, name from Account where name = 'test'];//list has no rows
}
catch( QueryException e ) {
System.debug( e.getMessage() );
}
//SObjectException
try {
Merchandise__c m = [SELECT Name FROM Merchandise__c LIMIT 1];
// Causes an SObjectException because we didn't retrieve
// the Total_Inventory__c field.
Double inventory = m.Total_Inventory__c;
} catch(SObjectException e) {
System.debug('The following exception has occurred: ' + e.getMessage());
}
I understood, find the example below:
Example:
Reference Link: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_types_catching.htm
If it helps you, please mark is as best answer, so it will be helpful for other developers.
All Answers
In your test class, you need to make some Error like QueryException, SObjectException that way the try will send to Catch method, then your code will be covered.
Thanks,
since i dont have any testclass knowledge .. you you provide code snippet for QueryException, SObjectException and dml exception
I understood, find the example below:
Example:
Reference Link: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_types_catching.htm
If it helps you, please mark is as best answer, so it will be helpful for other developers.