Skip to content

Exception Handling

Classification of Exceptions

Exceptions can be put in these categories:

  1. Unpredictable, technical exceptions, like NullPointerException or MethodNotFoundException, that are the symptom of a bug in the software
  2. Predictable, technical exceptions, often related to network connection and availability problems, e.g. TimeoutException or UnknownHostException
  3. Predictable, non-technical exceptions, also known as ‘Business Exceptions’

How to handle Exceptions

In total, the application could take three different kind of actions when an exception occurs:

  • tell the user that an exception has occurred and she cannot use the application anymore and terminate the current user session
  • tell the user that an exception has occurred but that she can continue to use the application
  • tell the user that a business exception has occurred and optionally what she could do to avoid/resolve this problem

There are two ways to tell the user about an exception:

  • stay on the current page and display a message, typically with p:growl or p:messages
  • navigate to an error page, either to recoverableError.xhtml or unrecoverableError.xhtml

You can directly navigate to an error page and explicitly add messages to be shown on the current page. As a handy alternative you can throw one of the exceptions UnrecoverableException and RecoverableException. There is a centralized exception handling available in AbstractManagedBean and in PrimeExceptionHandler, which do the message display and error page navigation in a generalized way. The only thing you need to take care of is to decide, which exception type to throw and how to parameterize it.

Configuration for Primefaces Exception Handler