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
meghna nmeghna n 

return statement in try catch block

I have a piece of code in apex controller as follows.
 @auraEnabled   
    public static List<Service_Request__c> showSomeData(String caseId,String srReason, String recordTypeName)
    {
         try {
               List<Service_Request__c> srList = [SELECT Id,name
                                           FROM Service_Request__c  
                                           WHERE Service_Request_Reason__c =:someCondition];
           return srList;
    }
         catch(Exception e){
         System.debug('Error from ApexController Method' + e.getMessage());
            
        }  
    }

I added the try catch block as above and when I save it shows an error as  " Missing return statement required return type: List<Service_Request__c>"

Please let me know I missing some syntaxt
Somya TiwariSomya Tiwari
Hi Meghna,

You can easily solve it by executing the return statement outside the try block. 
@auraEnabled   
    public static List<Service_Request__c> showSomeData(String caseId,String srReason, String recordTypeName)
    {
 List<Service_Request__c> srList = new List<Service_Request__c>();
         try {
               srList.addAll([SELECT Id,name
                                           FROM Service_Request__c  
                                           WHERE Service_Request_Reason__c =:someCondition]);
    }
         catch(Exception e){
         System.debug('Error from ApexController Method' + e.getMessage());
            
        }  
           return srList;
    }
Hope i helped you with your problem statement.
Deepali KulshresthaDeepali Kulshrestha
Hi meghna,
Greetings to you!

- You have to return after try-catch block also. Please use the below code [Solved] : -
@auraEnabled   
    public static List<Service_Request__c> showSomeData(String caseId,String srReason, String recordTypeName)
    {
        try {
              List<Service_Request__c> srList = [SELECT Id,name
                                           FROM Service_Request__c  
                                           WHERE Service_Request_Reason__c =:someCondition];
           return srList;
        }
        catch(Exception e){
        System.debug('Error from ApexController Method' + e.getMessage());    
        }  
        return null;
    }

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Khan AnasKhan Anas (Salesforce Developers) 
Hi Meghna,

Greetings to you!

1. You can write a return statement after the completion of try-catch blocks; that is just before the end of a method - This is a valid case because, after try-catch-finally block execution, the method returns value.
@auraEnabled   
    public static List<Service_Request__c> showSomeData(String caseId,String srReason, String recordTypeName)
    {
         List<Service_Request__c> srList
         try {
               srList = [SELECT Id,name
                                           FROM Service_Request__c  
                                           WHERE Service_Request_Reason__c =:someCondition];
           //return srList;
    }
         catch(Exception e){
         System.debug('Error from ApexController Method' + e.getMessage());
            
        }  
        return srList;
    }

2. You can write a return statement inside both try-block & catch-block - Whenever try-block executes successfully, then it can return value for this method
Also, if any exception is raised from try-block then its corresponding exception will be caught in the catch-block
And from catch-block also, it can return value for this method.
@auraEnabled   
    public static List<Service_Request__c> showSomeData(String caseId,String srReason, String recordTypeName)
    {
         List<Service_Request__c> srLis;
         try {
               srList = [SELECT Id,name
                                           FROM Service_Request__c  
                                           WHERE Service_Request_Reason__c =:someCondition];
           return srList;
    }
         catch(Exception e){
         System.debug('Error from ApexController Method' + e.getMessage());
         return srList;
        }  
        
    }

3. You can write return statement inside try-block & at the end of the method; that is just before the end of the method - Whenever try-block executes successfully, then it can always return value for this method
But if any exception is raised & it is handled in the corresponding catch-block –> return statement at the end of method will be executed and returns the value for this method.
@auraEnabled   
    public static List<Service_Request__c> showSomeData(String caseId,String srReason, String recordTypeName)
    {
         List<Service_Request__c> srList
         try {
               srList = [SELECT Id,name
                                           FROM Service_Request__c  
                                           WHERE Service_Request_Reason__c =:someCondition];
           return srList;
    }
         catch(Exception e){
         System.debug('Error from ApexController Method' + e.getMessage());
            
        }  
        return srList;
    }

4. You can write return statement inside catch-block & at the end of the method; that is just before the end of the method - Whenever try-block executes successfully, then it can always return value from end of the method
If any exception is raised from try-block then it get caught in the corresponding catch-block and catch-block can also return value
But if any exception is raised & it is handled in the corresponding catch-block –> return statement at the end of method will be executed and returns value for this method.
@auraEnabled   
    public static List<Service_Request__c> showSomeData(String caseId,String srReason, String recordTypeName)
    {
         List<Service_Request__c> srList
         try {
               srList = [SELECT Id,name
                                           FROM Service_Request__c  
                                           WHERE Service_Request_Reason__c =:someCondition];
           //return srList;
    }
         catch(Exception e){
         System.debug('Error from ApexController Method' + e.getMessage());
         return srList;
        }  
        return srList;
    }

Please refer to the below links which might help you further.

http://www.benchresources.net/returning-value-from-method-having-try-catch-finally-blocks-in-java/

I hope it helps you.

Kindly 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. It will help to keep this community clean.

Thanks and Regards,
Khan Anas