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
Nick JosephNick Joseph 

Catch Block Coverage

I have searched the formus and found several examples of Test Coverage for Catch blocks, however I have no been able to implement a solution that successfully covers it. Here is a quick code snippet of the Try/Catch from the Controller:

datacase = [SELECT ID, Subject, Recently_Escalated__c, Type, OwnerID, Data_Case_Description__c, Status, Description, Priority, Severity__c FROM Case WHERE Recently_Escalated__c = TRUE AND Type = 'Escalated'];
        try {
        supportcase = [SELECT ID, Subject, Recently_Escalated__c, Type, OwnerID, Description, Status, Priority, Severity__c, Data_Case_Description__c  FROM Case WHERE Recently_Escalated__c = TRUE AND Status = 'New'];
        } catch (Exception ex) {
            supportcase = null;

Test Class snippet:
 Case testSupportCase = New Case (
            Subject = 'Test Subject2',
            Type = 'Question',
            Recently_Escalated__c = TRUE,
            OwnerID = testOwnerID,
            Data_Case_Description__c = 'Test2',
            Description = 'Test2',
            Priority = 'High',
            Severity__c = 'Urgent',
            Status = 'New'

 Test.startTest();
            List<Case> newCaseList = new List<Case>();
            controller.resetCases();
            newCaseList = [SELECT ID, Recently_Escalated__c FROM CASE WHERE Recently_Escalated__c=TRUE];
            delete testSupportCase;
            controller.getSupportCase();
        Test.stopTest();  
Swayam@SalesforceGuySwayam@SalesforceGuy
To Achive this you have create another test method and has to throw some exception providing some error out value and using 
 
//some other code
yourClass.someMethod('Hello');
yourClass.someMethod(null);
//some other code


 try{
        //Call Some method
     }
      
       catch(Exception ex){
           System.Assert(ex.getMessage().contains('Message you are expecting'));           
       }        
Hope this helps.

---
Thanks
Swayam