Interface ExceptionHandler


public interface ExceptionHandler
Interface for custom exception handlers used with ExceptionCatch.

Implementations can be registered as beans and referenced by name in the @ExceptionCatch annotation.

What handleException throwing actually does: anything it throws propagates to the caller of the intercepted method, including a checked Throwable the intercepted method's own throws clause does not declare - ExceptionInterceptor does not catch or wrap it. There is no distinction between a handler that deliberately re-threw and one whose body failed by accident: both reach business code the same way. A handler implementation that assumes an accidental failure falls back to the annotation's default value is wrong about this interface's contract - write handleException bodies with that in mind, or catch what you do not want to propagate before it leaves this method.

The annotation's default value (@ExceptionCatch(defaultValue = ...)) is used instead of a handler's result only when: no handler is configured, the configured handler bean name cannot be resolved from the container, the resolved bean does not implement this interface, or handleException returns normally.

Self-invocation: a call a bean makes to its own method is intercepted here exactly as an external call is - this codebase's proxy is instantiated directly as the bean (there is no separate delegate target instance), so self-invocation is not the AOP-bypass gap it is for a delegating proxy. See ProxyFactory's class javadoc.

Since:
6.2.0
Author:
wisdomme
  • Method Summary

    Modifier and Type
    Method
    Description
    default int
    Gets the order of this handler.
    handleException(Throwable exception, Object target, Method method, Object[] args)
    Handles an exception caught during method execution.
    default boolean
    supports(Class<? extends Throwable> exceptionType)
    Determines if this handler supports handling the given exception type.
  • Method Details

    • handleException

      Object handleException(Throwable exception, Object target, Method method, Object[] args) throws Throwable
      Handles an exception caught during method execution.

      Whatever this method throws - checked or unchecked, declared on the intercepted method or not - propagates to the caller unconditionally. See the interface javadoc for the full semantics, including why a handler failure and a deliberate re-throw are indistinguishable.

      Parameters:
      exception - the exception that was caught
      target - the target object whose method threw the exception
      method - the method that threw the exception
      args - the arguments passed to the method
      Returns:
      a replacement return value, or null
      Throws:
      Throwable - if the handler decides to re-throw the exception, or if the handler's own body fails - both propagate identically, see the interface javadoc
    • supports

      default boolean supports(Class<? extends Throwable> exceptionType)
      Determines if this handler supports handling the given exception type.
      Parameters:
      exceptionType - the type of exception
      Returns:
      true if this handler can handle the exception
    • getOrder

      default int getOrder()
      Gets the order of this handler. Lower values have higher priority.
      Returns:
      the handler order