The recommended approach

We recommend an approach very similar to the one used by Spring Framework. When deciding what exceptions to throw from a method, always think about the consumer of the method.

Can the consumer of the method do something about the exception?

In the preceding example, if the execution of the query failed, the consumer method would not be able to do anything except show an error page to the user. In that kind of a scenario, we should not complicate things and force the consumer to handle the exception.

We recommend the following approach to exception handling in applications:

  • Think about the consumer. If the consumer of the method cannot do anything useful (except logging or showing an error page) about the exception, ...

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.