09:32

Spring MVC Exception Handling using @ControllerAdvice annotation

Spring MVC provides a great way to handle exceptions and errors. @ExceptionHandler annotation is core to this feature. For each Spring controller we can simply define a method that automatically gets called if a given exception occurs. For example:
import org.springframework.web.bind.annotation.ExceptionHandler; //.. @ExceptionHandler(IOException.class) public String exception(Exception e) {               //..     return "error"; } Thus whenever an IOException is raised from any controller method will call the above method exception(). We mapped IOException.class to this method using @ExceptionHandler annotation.

One short coming of this annotation is that it only handles exception getting raised from the controller where it is defined. It will not handle exceptions getting raised from other controllers. However this is a way to overcome this [.....]

Next Post Newer Posts Previous Post Older Posts Home