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
Saru VadlamudiSaru Vadlamudi 

How to cover catch exception in test class

Can any one help me out that how to cover the code coverage for below catch excpetion in test class.
I dont have permission to change Apex Controller so it has to do something in test class itself.

catch(Exception e) {
         new ApexPages.Message(ApexPages.Severity.Error,'Easy Button Config Custom Setting doesn\'t exist!');
     } 
Chandra Sekhar CH N VChandra Sekhar CH N V
In your testmethod, insert records in such a way that it will cause the exception to occur. This will cover the exception lines as well.
Dilip_VDilip_V
Hi saru,

try this in your test class.

try
      {
         //perform actions like insert,update,delete...etc that causes your exception to occur
          system.assert(false);
       }
       Catch(exception e)
       {
       
       }
satish marisetty 10satish marisetty 10
 public static Boolean checkFaultAnalysis(String stockingRecordId,String mmId){
        
        Boolean faultAnalysisFlag = false;
           try{
               if(!String.IsEmpty(stockingRecordId) && !String.isEmpty(mmId)){
                  
                  List<ICS_WR_Customer_Warranty_Config_Matrix__c> faultMatrix = [SELECT id,Name,RecordTypeId FROM ICS_WR_Customer_Warranty_Config_Matrix__c WHERE ICS_WR_Stocking__c=:stockingRecordId AND ICS_WR_MMID__r.Name=:mmId AND RecordTypeId=:Label.ICS_WR_Fault_Analysis_RT_ID];
                   System.debug('faultMatrix ='+faultMatrix );
                   if(faultMatrix!=null && faultMatrix.size()>0)
                      faultAnalysisFlag = true;
                       
               }
           }catch(Exception e){
           
             System.Debug(e.getMessage());
            ApexPages.addMessages(e);
            String strInvocationID ='ICS_WR_Warranty_CreateExtension'+UserInfo.getUserId() + '_' + system.now().formatGmt('yyyyMMddkkmmssSS')
                                        + '_' + String.valueOf((Math.random() * 10).round()).leftpad(3,'0');
             Core_Log_Entry.logEntryWithException(strInvocationID,System.Label.ICS_WR_logging_Scope,'ICS_WR_Warranty_CreateExtension__checkFaultAnalysis','Error',e.getMessage(),'',e);
           }
        return faultAnalysisFlag;
      }