[SalesForce] Try and Catch in apex

I am using one try-catch block inside which there are multiple SOQL queries WITH SECURITY_ENFORCED. Do I need try catch block 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<Account> acc = [SELECT id, Name FROM Account WITH SECURITY_ENFORCED];
 List<Contact> con = [SELECT id, LastName FROM Contact WITH SECURITY_ENFORCED];
//somelogic here
}catch(Exception e){
System.debug('Error--'+e.getMessage());
}
}

Best Answer

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

Related Topic