@ExceptionHandler can catch several exceptions in the same method. To do this, you need to pass several exception types inside this annotation and pass a superclass of these exceptions to the parameters of the @ExceptionHandler method.
Imagine that there are two types of exceptions: FlightNotFoundException, which is thrown in FlightController, and TicketNotFoundException thrown in TicketController. Both FlightNotFoundException and TicketNotFoundException extend RuntimeException.
You need a handler that will handle these two exceptions (only them) in one method and return a response body with the NOT_FOUND status code. For each exception, the response body must contain its own message.
Choose the correct way to handle the exceptions.
1)
Java
@ControllerAdvice
public class ControllerExceptionHandler {
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<Object> handleFlightAndTicketNotFound(
TicketNotFoundException e1, FlightNotFoundException e2) {
Map<String, Object> body = new HashMap<>();
body.put("status", HttpStatus.NOT_FOUND.value());
body.put("message", e1.getMessage());
return new ResponseEntity<>(body, HttpStatus.NOT_FOUND);
}
}
Kotlin
@ControllerAdvice
class ControllerExceptionHandler {
@ExceptionHandler(RuntimeException::class)
fun handleFlightAndTicketNotFound(
e1: TicketNotFoundException, e2: FlightNotFoundException
): ResponseEntity<Any> {
val body: MutableMap<String, Any> = HashMap()
body["status"] = HttpStatus.NOT_FOUND.value()
body["message"] = e1.getMessage()
return ResponseEntity(body, HttpStatus.NOT_FOUND)
}
}
2)
Java
@ControllerAdvice
public class ControllerExceptionHandler {
@ExceptionHandler({TicketNotFoundException.class, FlightNotFoundException.class})
public ResponseEntity<Object> handleFlightAndTicketNotFound(RuntimeException e) {
Map<String, Object> body = new HashMap<>();
body.put("status", HttpStatus.NOT_FOUND.value());
body.put("message", e.getMessage());
return new ResponseEntity<>(body, HttpStatus.NOT_FOUND);
}
}
Kotlin
@ControllerAdvice
class ControllerExceptionHandler {
@ExceptionHandler(TicketNotFoundException::class, FlightNotFoundException::class)
fun handleFlightAndTicketNotFound(e: RuntimeException): ResponseEntity<Any> {
val body: MutableMap<String, Any?> = HashMap()
body["status"] = HttpStatus.NOT_FOUND.value()
body["message"] = e.message
return ResponseEntity(body, HttpStatus.NOT_FOUND)
}
}
3)
Java
@ControllerAdvice
public class ControllerExceptionHandler {
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<Object> handleFlightAndTicketNotFound(RuntimeException e) {
Map<String, Object> body = new HashMap<>();
body.put("status", HttpStatus.NOT_FOUND.value());
body.put("message", e.getMessage());
return new ResponseEntity<>(body, HttpStatus.NOT_FOUND);
}
}
Kotlin
@ControllerAdvice
class ControllerExceptionHandler {
@ExceptionHandler(RuntimeException::class)
fun handleFlightAndTicketNotFound(e: RuntimeException): ResponseEntity<Any> {
val body: MutableMap<String, Any?> = HashMap()
body["status"] = HttpStatus.NOT_FOUND.value()
body["message"] = e.message
return ResponseEntity(body, HttpStatus.NOT_FOUND)
}
}
4)
Java
@ControllerAdvice
public class ControllerExceptionHandler {
@ExceptionHandler({TicketNotFoundException.class, FlightNotFoundException.class})
public ResponseEntity<Object> handleFlightAndTicketNotFound(
TicketNotFoundException e1, FlightNotFoundException e2) {
Map<String, Object> body = new HashMap<>();
body.put("status", HttpStatus.NOT_FOUND.value());
body.put("message", e2.getMessage());
return new ResponseEntity<>(body, HttpStatus.NOT_FOUND);
}
}
Kotlin
@ControllerAdvice
class ControllerExceptionHandler {
@ExceptionHandler(TicketNotFoundException::class, FlightNotFoundException::class)
fun handleFlightAndTicketNotFound(
e1: TicketNotFoundException, e2: FlightNotFoundException
): ResponseEntity<Any> {
val body: MutableMap<String, Any?> = HashMap()
body["status"] = HttpStatus.NOT_FOUND.value()
body["message"] = e2.message
return ResponseEntity(body, HttpStatus.NOT_FOUND)
}
}