[[webflux-ann-controller-exceptions]]
= Exceptions

[.small]#xref:web/webmvc/mvc-controller/ann-exceptionhandler.adoc[See equivalent in the Servlet stack]#

`@Controller` and xref:web/webflux/controller/ann-advice.adoc[@ControllerAdvice] classes can have
`@ExceptionHandler` methods to handle exceptions from controller methods. The following
example includes such a handler method:

[tabs]
======
Java::
+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
----
	@Controller
	public class SimpleController {

		// ...

		@ExceptionHandler // <1>
		public ResponseEntity<String> handle(IOException ex) {
			// ...
		}
	}
----
<1> Declaring an `@ExceptionHandler`.

Kotlin::
+
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
----
	@Controller
	class SimpleController {

		// ...

		@ExceptionHandler // <1>
		fun handle(ex: IOException): ResponseEntity<String> {
			// ...
		}
	}
----
<1> Declaring an `@ExceptionHandler`.
======



The exception can match against a top-level exception being propagated (that is, a direct
`IOException` being thrown) or against the immediate cause within a top-level wrapper
exception (for example, an `IOException` wrapped inside an `IllegalStateException`).

For matching exception types, preferably declare the target exception as a method argument,
as shown in the preceding example. Alternatively, the annotation declaration can narrow the
exception types to match. We generally recommend being as specific as possible in the
argument signature and to declare your primary root exception mappings on a
`@ControllerAdvice` prioritized with a corresponding order.
See xref:web/webmvc/mvc-controller/ann-exceptionhandler.adoc[the MVC section] for details.

NOTE: An `@ExceptionHandler` method in WebFlux supports the same method arguments and
return values as a `@RequestMapping` method, with the exception of request body-
and `@ModelAttribute`-related method arguments.

Support for `@ExceptionHandler` methods in Spring WebFlux is provided by the
`HandlerAdapter` for `@RequestMapping` methods. See xref:web/webflux/dispatcher-handler.adoc[`DispatcherHandler`]
for more detail.



[[webflux-ann-exceptionhandler-args]]
== Method Arguments
[.small]#xref:web/webmvc/mvc-controller/ann-exceptionhandler.adoc#mvc-ann-exceptionhandler-args[See equivalent in the Servlet stack]#

`@ExceptionHandler` methods support the same xref:web/webflux/controller/ann-methods/arguments.adoc[method arguments]
as `@RequestMapping` methods, except the request body might have been consumed already.



[[webflux-ann-exceptionhandler-return-values]]
== Return Values
[.small]#xref:web/webmvc/mvc-controller/ann-exceptionhandler.adoc#mvc-ann-exceptionhandler-return-values[See equivalent in the Servlet stack]#

`@ExceptionHandler` methods support the same xref:web/webflux/controller/ann-methods/return-types.adoc[return values]
as `@RequestMapping` methods.



