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
Vishnu SanthoshVishnu Santhosh 

Will the Outer methods with Try-Catch block catches exception of Inner methods ?

Hi,

I am new to Apex Development and am on the way of learning and discovering it more.

Consider this scenario : 
 

Class { 

       Method 1 {
                              try{
                                     //Statement 1
                                     //Statement 2 ......
                                     method2(params)
                                      ...............
                                    }
                                catch(Exception ex) { 
                                       //throws exception here
                                 }
         }

         Method 2 {
                         //Some statements
          }


Will the Try- catch block in Method 1 catches exception if there is any exception/error thrown in the method 2 ?

New to this. Correct me if i am wrong :)

Thank you

Best Answer chosen by Vishnu Santhosh
ShivankurShivankur (Salesforce Developers) 
Hi Vishnu,

You might need to make a call to the method 2 from method 1 so that Try- catch block in Method 1 catches exception if there is any exception/error thrown in the method 2 .

It will finally depend on your business requirement and you will need to be sure you are following best practices to avoid any other issues coming up while customizing such thing.

Reference:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_trycatch_example.htm
https://www.levelupsalesforce.com/apex-try-catch

Hope above information helps. Please mark as Best Answer so that it can help others in future.

Thanks.

All Answers

ShivankurShivankur (Salesforce Developers) 
Hi Vishnu,

You might need to make a call to the method 2 from method 1 so that Try- catch block in Method 1 catches exception if there is any exception/error thrown in the method 2 .

It will finally depend on your business requirement and you will need to be sure you are following best practices to avoid any other issues coming up while customizing such thing.

Reference:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_trycatch_example.htm
https://www.levelupsalesforce.com/apex-try-catch

Hope above information helps. Please mark as Best Answer so that it can help others in future.

Thanks.
This was selected as the best answer
Vishnu SanthoshVishnu Santhosh
Hi Shivankur,

Method 1 is calling the Method 2 as " method2(params)". Sorry for not mentioning that.

So, Method 1 will catch any errors thrown at Method 2 i believe. Got it.
Thank you so much for answering :)