dev@glassfish.java.net

Re: using NavigationHandler in ExceptionHandlerWrapper

From: Roger Kitain <roger.kitain_at_oracle.com>
Date: Thu, 04 Aug 2011 16:40:52 -0400

Assuming the PostConstruct is on a managed bean...
The specification does mention (w/r/t Postconstruct):
"If the method throws an unchecked
exception, the JSF implementation must not put the managed-bean into
service, a message must be logged, and further methods on that managed
bean instance must not be called".
Does not really say anything w/r/t ExceptionHandler like it does for
PreDestroy (below)...

Also, w/r/t PreDestroy: Section 6.2.1 of the specification says if an
exception occurs in PreDestroy
it must not be handled by ExceptionHandler.




On 8/4/11 4:17 PM, Derek Knapp wrote:
> I see, the problem is when the exception is thrown in the
> PostConstruct, if its in a button's action, it works properly..
>
> Is there any way around this problem?
>
>
> Derek
>
>
> On 11-08-04 03:57 PM, Derek Knapp wrote:
>> Why will the following code not redirect to my error page? I have
>> been Googling forever, with no luck :(
>>
>>
>>
>>
>> package com.itrac.exception;
>>
>> import java.util.Iterator;
>> import javax.faces.FacesException;
>> import javax.faces.application.NavigationHandler;
>> import javax.faces.context.ExceptionHandler;
>> import javax.faces.context.ExceptionHandlerWrapper;
>> import javax.faces.context.FacesContext;
>> import javax.faces.event.ExceptionQueuedEvent;
>> import javax.faces.event.ExceptionQueuedEventContext;
>>
>> /**
>> *
>> * @author derek
>> */
>> public class ItracExceptionHandler extends ExceptionHandlerWrapper
>> {
>> private ExceptionHandler wrapped;
>>
>> public ItracExceptionHandler(ExceptionHandler wrapped)
>> {
>> this.wrapped = wrapped;
>> }
>>
>> @Override
>> public ExceptionHandler getWrapped()
>> {
>> return wrapped;
>> }
>>
>> @Override
>> public void handle() throws FacesException
>> {
>> for (Iterator<ExceptionQueuedEvent> i =
>> getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();)
>> {
>> ExceptionQueuedEvent event = i.next();
>> ExceptionQueuedEventContext context =
>> (ExceptionQueuedEventContext) event.getSource();
>> Throwable t = context.getException();
>>
>> // redirect to error page
>> FacesContext fc = FacesContext.getCurrentInstance();
>> NavigationHandler nav =
>> fc.getApplication().getNavigationHandler();
>> nav.handleNavigation(fc, null, "/error");
>> fc.renderResponse();
>>
>> // remove it if you processed it
>> i.remove();
>> }
>>
>> // let the next ExceptionHandler(s) deal with the others
>> getWrapped().handle();
>> }
>> }
>>


-- 
roger.kitain_at_oracle.com
https://twitter.com/rogerk09
http://www.java.net/blogs/rogerk