Annotation Type ExceptionHandler


@Documented @Target(METHOD) @Retention(SOURCE) public @interface ExceptionHandler
Annotation for handling exceptions in controller classes and/or handler methods.

Handler methods which are annotated with this annotation are allowed to have very flexible signatures. They may have parameters of the following types:

  1. An exception argument: declared as a general Exception or as a more specific exception. This also serves as a mapping hint if the annotation itself does not narrow the exception types through its value().
  2. Request and/or response objects (typically from the microframework). You may choose any specific request/response type, e.g. Javalin's io.javalin.Context or Helidon's ServerRequest/ServerResponse.

Handler methods may be void or return an object for serialization.

You may combine the ExceptionHandler annotation with @Produces for a specific HTTP error status and media type.

  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    int
    The response status code to use.
    Class<? extends Exception>
    Exception handled by the annotated method.
  • Element Details

    • value

      Class<? extends Exception> value
      Exception handled by the annotated method. If empty, will default to any exception listed in the method argument list.
      Default:
      io.avaje.http.api.DefaultException.class
    • statusCode

      int statusCode
      The response status code to use.
      Default:
      0