[SalesForce] How to cover catch block in test class in salesforce

Below apex class is not covering catch block. Due to thi i'm unable to migrate the below class to other environments.Can anyone help me to cover the catch block.
Now it's covered 75%.
Apex Class:

    @AuraEnabled
        public static List<RecordType> getListOfRecordType(){
            String businessRT = Label.Account_RecordType_Business_Devname;
            String partnerRT =  Label.Account_RecordType_COI_Devname;        
            try{
                 return [SELECT Id, toLabel(Name), toLabel(Description) FROM RecordType 
                                        WHERE (IsActive=TRUE AND 
                                            SobjectType = 'Account' AND
                                            Name != 'Master') AND
                                            (DeveloperName = :businessRT OR DeveloperName = :partnerRT)
                                            ];

            }catch(Exception e){
                return null; 
            }    
        }

    }

Apex Test Class:

    static testMethod void RecordTypeSelectorControllerTestMethod() {
            List<Trigger_Bypass__c> tSwitch = TestDataFactory.createTriggerSwitch();              
            Database.insert(tSwitch);           
            Schema.DescribeSObjectResult recordType = Schema.SObjectType.Account; 
            Map<String,Schema.RecordTypeInfo> AccountRecordTypeInfo = recordType.getRecordTypeInfosByName();
            Id rtId = AccountRecordTypeInfo .get('Business').getRecordTypeId();        
            Account Acc = new Account(Name='Master',recordtypeid=AccountRecordTypeInfo .get('Business').getRecordTypeId());       
            insert Acc;
            Test.startTest();        
            RecordTypeSelectorController.getListOfRecordType();
            Test.stopTest();
        }

Best Answer

There's no exception here you could possibly catch; it is recommended that you remove the try-catch block. The fact that you can't write a proper unit test that covers it is proof that you can't catch anything here.

However, if you were to run in to a situation like this where a try-catch would be effective, you'd want to move the return value to a variable that you'd return at the end, like this:

@AuraEnabled
    public static List<RecordType> getListOfRecordType(){
        String businessRT = Label.Account_RecordType_Business_Devname;
        String partnerRT =  Label.Account_RecordType_COI_Devname;
        RecordType[] results;        
        try{
             results = [SELECT Id, toLabel(Name), toLabel(Description) FROM RecordType 
                                    WHERE (IsActive=TRUE AND 
                                        SobjectType = 'Account' AND
                                        Name != 'Master') AND
                                        (DeveloperName = :businessRT OR DeveloperName = :partnerRT)                ];

        }catch(Exception e){
        }
        return results;
    }
}
Related Topic