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
hiteshwar marnihiteshwar marni 

exception handling best practises

what are the best practises for Exception handling in salesforce.I have gone through SF Document but didn't find any where.
Thanks
Parag Bhatt 10Parag Bhatt 10
Hey! you can check the following link for the best exception handling practise .this link help you learn step by step. also explains Basic error handling and Custom error handling Just check the link below

https://developer.salesforce.com/blogs/2017/09/error-handling-best-practices-lightning-apex.html

thanks
Parag Bhatt
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same
1) Example Handling
 https://developer.salesforce.com/page/An_Introduction_to_Exception_Handling

2) Exception Handling Example
 https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_trycatch_example.htm

3) Error Handling Best Practices for Lightning and Apex
https://developer.salesforce.com/blogs/2017/09/error-handling-best-practices-lightning-apex.html


Best Practices for Exception Handling in Apex
http://www.brcline.com/blog/exception-handling-in-apex
  • Never just catch Exception, instead catch the specific type of exceptions. For example, QueryException should be caught if you know it’s possible you might not have any records in the query. I commonly use a pattern like this for finding if there’s any associated tasks:
  • Exceptions in Apex should be handled carefully as they may mask some serious problems that may not be caught for a long time. Try catch blocks should be used sparingly because they can cause some serious data integrity issues.
  • Don’t be scared to use the System.SavePoint() and Database.rollback() functions.The key advantage of doing a rollback is that we can set the database back to a known state and not necessarily worry about whether an admin adds a validation rule or not.
  • Proper exception handling is usually a lot more than using try-catch blocks, because it isn’t necessarily bulkified. In general, library classes and triggers should contain very few if any exception handling.
  • try-catch blocks should very small as the larger the block is the more errors it is likely to be hiding.

Let us know if this will help you
 
Jonathan AndersonJonathan Anderson
Something else to bear in mind for exception handling that you must do something with every handled exception. Either you are notified about it and can investigate or you correct the issue in code. Just outputting debug is a very bad idea. I'm not aware of any solution right now that lets you get notifications based on these. 

There are further considerations for trying to fix an issue in code: you may expect a DMLException for, say, an insert statement, and write your code to act on this, but then you get a DMLException for an update you made earlier which you never suspected would be a problem. If there is any chance of ambiguity over this, it is best to get a notification of some sort. Either send an email out or create a custom 'exception data' object to log the problem and monitor these.

It appears that not all unhandled exceptions will result in emails. Scheduled Apex jobs do not seem to send emails when there is an unhandled exception. This probably means you need to use try/catch more in these Apex classes than you might otherwise wish.