I am trying to define a generic error handling for my apex application. I have my try/catch blocks in controllers/extensions at various methods. I am able to check and catch business logic related exceptions ( and all other checked exceptions ). My question is related to unhandled or run time exceptions.

Is there a way to handle run time exceptions at a central level and show a generic error page (instead of system error page shown at present). Error handling needs to be elegant.

I know of similar features in java but was wondering if any such feature is available in apex as well.

Any pointers will be greatly appreciated.

2 Answers 2


You should be able to "predict" some of the run-time exceptions related to the platform governor limits by using the Limits Class, however I haven't seen anything yet that allows to handle a generic run-time exception.

  • Thanks for the answer. I am referring to generic error page which can be shown for anything that goes unhandled. Checking Limits and doing thorough code review will help in identifying probable exception scenarios but is not feasible for whole code base.
    – Dave
    Commented Aug 17, 2015 at 4:23
  • At this point in time you can only customize Site/Community Error pages, but not the internal ones. Apparently it's on the roadmap (since 2008) and it's highly prioritized as per the first answer here - developer.salesforce.com/forums/?id=906F000000094JPIAY . Commented Aug 17, 2015 at 4:29

Yeah, unfortunately, there are many "uncatchable" exceptions generated by Salesforce. Implementing Database.RaisesPlatformEvents will allow you to handle all exceptions thrown from a batch process - even the "uncatchable" ones. But other than that, the best shot you've got is to have disciplined development processes and follow best practices to avoid them.

Error handling / code quality is a personal passion of mine. I've created a library (Apex BOOST) that might provide you with some helpful boilerplate error handling. It includes persistent logging that works even if an exception rolls back the transaction, custom debug and assert methods with some huge benefits, and lots of productivity utilities. I'd love to get your impressions if you're interested in taking a look.

  • N.B. TransactionFinalizers for queuables
    – cropredy
    Commented Dec 27, 2022 at 18:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .