Class ExceptionHandler
- java.lang.Object
-
- jakarta.faces.context.ExceptionHandler
-
- All Implemented Interfaces:
FacesListener,SystemEventListener,EventListener
- Direct Known Subclasses:
ExceptionHandlerWrapper
public abstract class ExceptionHandler extends Object implements SystemEventListener
ExceptionHandler is the central point for handling unexpected
Exceptions that are thrown during the Faces lifecycle. TheExceptionHandlermust not be notified of anyExceptions that occur during application startup or shutdown.See the Jakarta Faces Specification Document for the requirements for the default implementation.
Exceptions may be passed to theExceptionHandlerin one of two ways:-
by ensuring that
Exceptions are not caught, or are caught and re-thrown.This approach allows the
ExceptionHandlerfacility specified in section 6.2 "ExceptionHandler" of the Jakarta Faces Specification Document to operate on theException. -
By using the system event facility to publish an
ExceptionQueuedEventthat wraps theException.This approach requires manually publishing the
ExceptionQueuedEvent, but allows more information about theExceptionto be stored in the event. The following code is an example of how to do this.//... } catch (Exception e) { FacesContext ctx = FacesContext.getCurrentInstance(); ExceptionQueuedEventContext eventContext = new ExceptionQueuedEventContext(ctx, e); eventContext.getAttributes().put("key", "value"); ctx.getApplication().publishEvent(ExceptionQueuedEvent.class, eventContext); }Because the
Exceptionmust not be re-thrown when using this approach, lifecycle processing may continue as normal, allowing moreExceptions to be published if necessary.
With either approach, any
ExceptionQueuedEventinstances that are published in this way are accessible to thehandle()method, which is called at the end of each lifecycle phase, as specified in section 6.2 "ExceptionHandler" of the Jakarta Faces Specification Document.Note that if
handle()happens to be invoked duringPhaseId.RENDER_RESPONSE, the recovery options are more limited than when it is invoked during other phases. Specifically, it is not valid to callNavigationHandler.handleNavigation(jakarta.faces.context.FacesContext, java.lang.String, java.lang.String)duringRENDER_RESPONSE.Instances of this class are request scoped and are created by virtue of
FacesContextFactory.getFacesContext(java.lang.Object, java.lang.Object, java.lang.Object, jakarta.faces.lifecycle.Lifecycle)callingExceptionHandlerFactory.getExceptionHandler().- Since:
- 2.0
-
-
Constructor Summary
Constructors Constructor Description ExceptionHandler()
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description abstract ExceptionQueuedEventgetHandledExceptionQueuedEvent()Return the firstExceptionQueuedEventhandled by this handler.abstract Iterable<ExceptionQueuedEvent>getHandledExceptionQueuedEvents()The default implementation must return anIterableover allExceptionQueuedEvents that have been handled by thehandle()method.abstract ThrowablegetRootCause(Throwable t)Unwrap the argumenttuntil the unwrapping encounters an Object whosegetClass()is not equal toFacesException.classorjakarta.el.ELException.class.abstract Iterable<ExceptionQueuedEvent>getUnhandledExceptionQueuedEvents()Return anIterableover allExceptionQueuedEvents that have not yet been handled by thehandle()method.abstract voidhandle()Take action to handle theExceptioninstances residing inside theExceptionQueuedEventinstances that have been queued by calls toApplication().publishEvent(ExceptionQueuedEvent.class, eventContext).abstract booleanisListenerForSource(Object source)This method must returntrueif and only if this listener instance is interested in receiving events from the instance referenced by thesourceparameter.abstract voidprocessEvent(SystemEvent exceptionQueuedEvent)When called, the listener can assume that any guarantees given in the javadoc for the specificSystemEventsubclass are true.
-
-
-
Method Detail
-
handle
public abstract void handle() throws FacesExceptionTake action to handle the
Exceptioninstances residing inside theExceptionQueuedEventinstances that have been queued by calls toApplication().publishEvent(ExceptionQueuedEvent.class, eventContext). The requirements of the default implementation are detailed in section 6.2.1 "Default ExceptionHandler implementation" of the Jakarta Faces Specification Document.- Throws:
FacesException- if and only if a problem occurs while performing the algorithm to handle theException, not as a means of conveying a handledExceptionitself.- Since:
- 2.0
-
getHandledExceptionQueuedEvent
public abstract ExceptionQueuedEvent getHandledExceptionQueuedEvent()
Return the first
ExceptionQueuedEventhandled by this handler.- Returns:
- instance of
ExceptionQueuedEvent.
-
getUnhandledExceptionQueuedEvents
public abstract Iterable<ExceptionQueuedEvent> getUnhandledExceptionQueuedEvents()
Return an
Iterableover allExceptionQueuedEvents that have not yet been handled by thehandle()method.- Returns:
- the unhandled set of
ExceptionQueuedEvents.
-
getHandledExceptionQueuedEvents
public abstract Iterable<ExceptionQueuedEvent> getHandledExceptionQueuedEvents()
The default implementation must return an
Iterableover allExceptionQueuedEvents that have been handled by thehandle()method.- Returns:
- an
Iterableover allExceptionQueuedEvents.
-
processEvent
public abstract void processEvent(SystemEvent exceptionQueuedEvent) throws AbortProcessingException
When called, the listener can assume that any guarantees given in the javadoc for the specific
SystemEventsubclass are true.- Specified by:
processEventin interfaceSystemEventListener- Parameters:
exceptionQueuedEvent- theSystemEventinstance that is being processed.- Throws:
AbortProcessingException- if lifecycle processing should cease for this request.
-
isListenerForSource
public abstract boolean isListenerForSource(Object source)
This method must return
trueif and only if this listener instance is interested in receiving events from the instance referenced by thesourceparameter.- Specified by:
isListenerForSourcein interfaceSystemEventListener- Parameters:
source- the source that is inquiring about the appropriateness of sending an event to this listener instance.- Returns:
- the value as specified above
-
getRootCause
public abstract Throwable getRootCause(Throwable t)
Unwrap the argument
tuntil the unwrapping encounters an Object whosegetClass()is not equal toFacesException.classorjakarta.el.ELException.class. If there is no root cause,nullis returned.- Parameters:
t- passed-in wrappedThrowable.- Returns:
- unwrapped object.
- Throws:
NullPointerException- if argumenttisnull.- Since:
- 2.0
-
-