function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rajiv B 3Rajiv B 3 

Catch block code coverage


Below is the code coverage issu due to this not able to get required code coverage.

if you provide snippet code will highly appreciate..

User-added image
 
Best Answer chosen by Rajiv B 3
Gokula KrishnanGokula Krishnan
Hi Rajiv,

I understood, find the example below:

Example:
//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());    
}

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

Gokula KrishnanGokula Krishnan
Hi Rajiv,

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,
Rajiv B 3Rajiv B 3
Hi Gokula thanks for reply..
since i dont have any testclass knowledge .. you you provide code snippet for  QueryException, SObjectException  and dml exception 
Gokula KrishnanGokula Krishnan
Provide your try statement..
Rajiv B 3Rajiv B 3
Thanks for your efforts... Sorry to say can not provide code due to data policy....if you can you can give general example code will be fine... 
 
Gokula KrishnanGokula Krishnan
Hi Rajiv,

I understood, find the example below:

Example:
//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());    
}

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.
This was selected as the best answer