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
ethan huntethan hunt 

retunring null in catch block

Hi All,


Is retunring null in catch block best practice.......

public List<Sobject> getSharedRecordsOfSObject(String query, Set<Id> setSObjectId) {
              
        List<sObject> listShareResult = new List<sObject>();       
        try {
            if(query != null) {
                listShareResult = Database.query(query);
            }
        return listShareResult;
        }
        catch(QueryException ex) {
            system.debug(ex);
            return null;
        }
    }


Regards
bob_buzzardbob_buzzard
Handling exceptions only really makes sense if you can recover from the situation. If returning null means that the calling code will know there was a problem, then its fine.  If returning null means the calling code will assume there are no list shares, that probably isn't correct, as there may be some but the code failed trying to extract them. If it means the action the user is trying to perform is going to fail, you may be better letting the exception bubble up to the top level code and displaying it to them.