arun_kk
arun_kk

Reputation: 392

handle exception thrown from constructors in JSF using Handlers

Hello folks, Though this is my first question, Iam used to reading solutions in stackoverflow and am really pleased to get appropriate answers.

My question is : Is there any way to handle exception thrown from a constructor using some handlers (Instead of writing try and catch in every constructor) and redirect page to an error.xhtml. Iam using JSF2. And how i can identify the exception is thrown from constructor. Waiting for your reply.

Upvotes: 2

Views: 882

Answers (1)

kolossus
kolossus

Reputation: 20691

First of all, try to avoid doing anything in the constructor of a managed bean, it's unnecessary and ugly. Annotate a public, no-argument method with the @PostConstruct annotation and that method is guaranteed to run immediately after the managed bean has been instantiated.

To address your main issue, JSF provides an ExceptionHandler class that you can override for the benefit of custom exception handling. It's implemented in a semi-declarative way and applies globally, i.e. for the entire JSF application context. This tutorial provides a good guide on implementing the handler. From that tutorial, note that the ExceptionQueuedEvent object provides all the information available concerning the exception thrown including the JSF Phase in which the exception was thrown and even the component that generated the exception

Upvotes: 2

Related Questions