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
Ajith Selvaraj 2Ajith Selvaraj 2 

how to cover the code after catch block?

can anyone explain me how to cover the code after the ctach block?
and my code is 
 public static List<Category_Target__c> saveCategoryTargets(List<Category_Target__c> categoryTargets) {
        String brandTargetId = categoryTargets[0].Brand_Target__c;
            Brand_Target1__c brandTarget = [SELECT Id,Period__c FROM Brand_Target1__c WHERE Id =:  brandTargetId];
            Range__c[] ranges = [SELECT Id FROM Range__c WHERE Period__c = : brandTarget.Period__c];
        if(categoryTargets != null && categoryTargets.size() > 0) {
            String error='';
          try{
                insert categoryTargets;
            }
            catch(DmlException e){             
                for (Integer i = 0; i < e.getNumDml(); i++) {
                    error =+ e.getDmlMessage(i) +  '\n' ;
                }
                throw new AuraHandledException(error);             
            }catch(Exception e){
                throw new AuraHandledException(e.getMessage());
            }          
             if(ranges.size() > 0) {
                List<Category_Range_Target__c> categoryRangeTargets = new List<Category_Range_Target__c>();
                for(Category_Target__c categoryTarget : categoryTargets)  {
                    for(Range__c range : ranges) {
                        Category_Range_Target__c categoryRangeTarget = new Category_Range_Target__c();
                        categoryRangeTarget.Category_Target__c = categoryTarget.Id;
                        categoryRangeTarget.Range__c = range.Id;
                        categoryRangeTargets.add(categoryRangeTarget);
                    }
                }
                String error1='';
                try{
                    insert categoryRangeTargets;
                }
                catch(DmlException e){             
                    for (Integer i = 0; i < e.getNumDml(); i++) {
                        error1 =+ e.getDmlMessage(i) +  '\n' ;
                    }
                    throw new AuraHandledException(error1);             
                }catch(Exception e){
                    throw new AuraHandledException(e.getMessage());
                } 
            }
            categoryTargets = [SELECT Id,Target__c,Category__c, Brand_Target__c,Brand_Target__r.Target__c,  (SELECT Id, Target__c FROM Category_Range_Targets__r) FROM Category_Target__c WHERE Id IN :categoryTargets];            
        }
        return categoryTargets;
    }
ANUTEJANUTEJ (Salesforce Developers) 
Hi Ajit,

The only way to cover the code after catch block is to have a testcase scenario where there are no exceptions so that the try block is executed without exception and after that block of code after catch block is executed.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.