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
Romeo MirzacRomeo Mirzac 

Error Handling Best Practice?

I'm new to Apex and have a very general question:

* Should I put Try..Catch..Finally statements in ALL of my class methods?  Or,
* Should I only add the Try..Catch..Finally to the main method and not add any handling on the subroutines?

What's the best practice?
Best Answer chosen by Romeo Mirzac
Hargobind_SinghHargobind_Singh
I agree with Deepak on not putting try-catch in all your classes. 

A widely used best-practice is to "Throw Early Catch Late", which roughly translates to that you should try catch exceptions as late as possible, mostly to user-invoked or primary action methods.

Here is an interesting article, look at Exception Handling Best Practices section. This is based on Java, but does explain some principles and best practices: 

http://www.journaldev.com/1696/java-exception-handling-tutorial-with-examples-and-best-practices

All Answers

Deepak RamaDeepak Rama
Romeo,

There is no best practice with respect to Try--Catch--Finally statements.

It is not a good advice to put try--catch in all your class methods as each try catch block takes more time to execute. Encapsulate your logic (whereever needed) in try-catch if you expect that that block would throw runtime errors.
Hargobind_SinghHargobind_Singh
I agree with Deepak on not putting try-catch in all your classes. 

A widely used best-practice is to "Throw Early Catch Late", which roughly translates to that you should try catch exceptions as late as possible, mostly to user-invoked or primary action methods.

Here is an interesting article, look at Exception Handling Best Practices section. This is based on Java, but does explain some principles and best practices: 

http://www.journaldev.com/1696/java-exception-handling-tutorial-with-examples-and-best-practices
This was selected as the best answer
Romeo MirzacRomeo Mirzac
Thank you everyone!  This is the advise I was looking for