I recently noticed that some of my @ResquestMapping methods in spring RestController which throw an Exception in the method body, does not have the exception declared with throws or being caught within a catch block, and neither am I using a Global Exception Handler of any kind. For example:
@GetMapping("/get/{animalName}")
public AnimalModel getAnimalByName(@PathVariable String animalName) {
if(animal.findByBreed(animalName) == null)
throw new AnimalNotFoundException(animalName + " is not present in " + parkName);
return animal.findByBreed(animalName);
}
This is strictly against the Java syntax. Over here I have two questions:
- Is Spring injecting or defining the throws clause internally?
- If so, how does Spring manage to suppress the Java compiler error:
"unreported exception Exception; must be caught or declared to be thrown"