Here is the class for the exception:
Take a look at the following method that processes requests:
Java
@GetMapping("flights/{id}")
public FlightInfo getFlightInfo(@PathVariable int id) {
for (FlightInfo flightInfo : flightInfoList) {
if (flightInfo.getId() == id) {
return flightInfo;
}
}
throw new FlightNotFoundException("Flight not found for id =" + id);
}
Kotlin
@GetMapping("flights/{id}")
fun getFlightInfo(@PathVariable id: Int): FlightInfo {
if (id > flightInfoList.size) {
throw FlightNotFoundException("Flight not found for id =$id")
}
return flightInfoList[id]
}
How can you rewrite the code above without using the custom exception FlightNotFoundException?
Java
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
class FlightNotFoundException extends RuntimeException {
// ...
}
Kotlin
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
class FlightNotFoundException : RuntimeException() {
// ...
}