Package org.springframework.web.method.annotation

# Class ExceptionHandlerMethodResolver

java.lang.Object
org.springframework.web.method.annotation.ExceptionHandlerMethodResolver

public class ExceptionHandlerMethodResolver extends Object
Discovers @ExceptionHandler methods in a given class, including all of its superclasses, and helps to resolve a given `Exception` and `MediaType` requested by clients to combinations supported by a given `Method`.

This resolver will use the exception information declared as `@ExceptionHandler` annotation attributes, or as a method argument as a fallback. This will throw `IllegalStateException` instances if:

- No Exception information could be found for a method
- An invalid `MediaType` has been declared as `@ExceptionHandler` attribute
- Multiple handlers declare the same exception + media type mapping

Since:
  3.1
Author:
  Rossen Stoyanchev, Juergen Hoeller, Sam Brannen, Brian Clozel

## Constructor Summary

Constructors
Constructor
Description
`ExceptionHandlerMethodResolver(Class<?> handlerType)`
A constructor that finds `ExceptionHandler` methods in the given type.

## Method Summary

Modifier and Type
Method
Description
`boolean`
`hasExceptionMappings()`
Whether the contained type has any exception mappings.
`ExceptionHandlerMappingInfo`
`resolveExceptionMapping(Throwable exception, MediaType mediaType)`
Find a `Method` to handle the given Throwable for the requested `MediaType`.
`ExceptionHandlerMappingInfo`
`resolveExceptionMappingByExceptionType(Class<? extends Throwable> exceptionType, MediaType mediaType)`
Find a `Method` to handle the given exception type and media type.
`Method`
`resolveMethod(Exception exception)`
Find a `Method` to handle the given exception.
`Method`
`resolveMethodByExceptionType(Class<? extends Throwable> exceptionType)`
Find a `Method` to handle the given exception type.
`Method`
`resolveMethodByThrowable(Throwable exception)`
Find a `Method` to handle the given Throwable.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### ExceptionHandlerMethodResolver

public ExceptionHandlerMethodResolver(Class<?> handlerType)
A constructor that finds `ExceptionHandler` methods in the given type.

Parameters:
  `handlerType` - the type to introspect
Throws:
  `IllegalStateException` - in case of invalid or ambiguous exception mapping declarations

## Method Details

### hasExceptionMappings

public boolean hasExceptionMappings()
Whether the contained type has any exception mappings.

### resolveMethod

@Nullable public Method resolveMethod(Exception exception)
Find a `Method` to handle the given exception.

Uses `ExceptionDepthComparator` if more than one match is found.

Parameters:
  `exception` - the exception
Returns:
  a Method to handle the exception, or `null` if none found

### resolveMethodByThrowable

@Nullable public Method resolveMethodByThrowable(Throwable exception)
Find a `Method` to handle the given Throwable.

Uses `ExceptionDepthComparator` if more than one match is found.

Parameters:
  `exception` - the exception
Returns:
  a Method to handle the exception, or `null` if none found
Since:
  5.0

### resolveExceptionMapping

@Nullable public ExceptionHandlerMappingInfo resolveExceptionMapping(Throwable exception, MediaType mediaType)
Find a `Method` to handle the given Throwable for the requested `MediaType`.

Uses `ExceptionDepthComparator` and `MediaType.isMoreSpecific(MimeType)` if more than one match is found.

Parameters:
  `exception` - the exception
  `mediaType` - the media type requested by the HTTP client
Returns:
  a Method to handle the exception, or `null` if none found
Since:
  6.2

### resolveMethodByExceptionType

@Nullable public Method resolveMethodByExceptionType(Class<? extends Throwable> exceptionType)
Find a `Method` to handle the given exception type. This can be useful if an `Exception` instance is not available (for example, for tools).

Uses `ExceptionDepthComparator` if more than one match is found.

Parameters:
  `exceptionType` - the exception type
Returns:
  a Method to handle the exception, or `null` if none found

### resolveExceptionMappingByExceptionType

@Nullable public ExceptionHandlerMappingInfo resolveExceptionMappingByExceptionType(Class<? extends Throwable> exceptionType, MediaType mediaType)
Find a `Method` to handle the given exception type and media type. This can be useful if an `Exception` instance is not available (for example, for tools).

Parameters:
  `exceptionType` - the exception type
  `mediaType` - the media type requested by the HTTP client
Returns:
  a Method to handle the exception, or `null` if none found