Do I need apex try catch block separately for each SOQL or the query exception can be handled in one try-catch block?

221    Asked by Aashishchaursiya in Salesforce , Asked on Apr 18, 2023

I am using one try-catch block inside which there are multiple SOQL queries WITH SECURITY_ENFORCED. Do I need to try catch blocks separately for each SOQL or the query exception would be handled in that one try-catch block which I am using.

Sample Code:

Apex

public static void myMethod(some parameter){ try{ //do I need separate try-catch block to catch the Query exception or it will be caught by the catch I am using?? List acc = [SELECT id, Name FROM Account WITH SECURITY_ENFORCED]; List con = [SELECT id, LastName FROM Contact WITH SECURITY_ENFORCED]; //somelogic here }catch(Exception e){ System.debug('Error--'+e.getMessage()); } }
Answered by Aashna Saito

One apex try catch block is enough, unless you want to act differently depending on which query caused the exception.




Your Answer

Interviews

Parent Categories