Chapter 48. Handling Exceptions
Abstract
When possible, exceptions caught by a resource method should cause a useful error to be returned to the requesting consumer. JAX-RS resource methods can throw a
WebApplicationException
exception. You can also provide ExceptionMapper<E>
implementations to map exceptions to appropriate responses.
48.1. Overview of JAX-RS Exception Classes
Overview
In JAX-RS 1.x, the only available exception class is
WebApplicationException
. Since JAX-WS 2.0, however, a number of additional JAX-RS exception classes have been defined.
JAX-RS runtime level exceptions
The following exceptions are meant to be thrown by the JAX-RS runtime only (that is, you must not throw these exceptions from your application level code):
ProcessingException
- (JAX-RS 2.0 only) The
javax.ws.rs.ProcessingException
can be thrown during request processing or during response processing in the JAX-RS runtime. For example, this error could be thrown due to errors in the filter chain or interceptor chain processing. ResponseProcessingException
- (JAX-RS 2.0 only) The
javax.ws.rs.client.ResponseProcessingException
is a subclass ofProcessingException
, which can be thrown when errors occur in the JAX-RS runtime on the client side.
JAX-RS application level exceptions
The following exceptions are intended to be thrown (and caught) in your application level code:
WebApplicationException
- The
javax.ws.rs.WebApplicationException
is a generic application level JAX-RS exception, which can be thrown in application code on the server side. This exception type can encapsulate a HTTP status code, an error message, and (optionall) a response message. For details, see Section 48.2, “UsingWebApplicationException
exceptions to report errors”. ClientErrorException
- (JAX-RS 2.0 only) The
javax.ws.rs.ClientErrorException
exception class inherits fromWebApplicationException
and is used to encapsulate HTTP4xx
status codes. ServerErrorException
- (JAX-RS 2.0 only) The
javax.ws.rs.ServerErrorException
exception class inherits fromWebApplicationException
and is used to encapsulate HTTP5xx
status codes. RedirectionException
- (JAX-RS 2.0 only) The
javax.ws.rs.RedirectionException
exception class inherits fromWebApplicationException
and is used to encapsulate HTTP3xx
status codes.