Exception handling is very essential feature of any java program. Every good open source framework allows to write the exception handlers in such a way that you can separate then from your application code. Well, Spring 3 also allows you to do so using annotation @ExceptionHandler. Lets see how all this works.
For demonstration, I am adding exception handler in already build code for this article [related to implementing interceptors in spring 3]. So in case any query to project files, please refer to linked post.
Download source code for this article
Adding exception handler
For this all you need is to define a method in your controller class and use the annotation @ExceptionHandler on it. Spring’s configuration will detect this annotation and register the method as exception handler for given exception and all of its subclasses. There are as such no other guidelines only related to method request and response parameters.
@ExceptionHandler(NullPointerException.class)
public ModelAndView handleException(NullPointerException ex)
{
//Do something additional if required
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("error");
modelAndView.addObject("message", ex.getMessage());
return modelAndView;
}
Now every time, controller encounter above exception in request processing for any web request, control will automatically come to this handler method.
For example, below URL is intentionally returning NullPointerException.
@RequestMapping(value="/demo/not-exist", method = RequestMethod.GET, headers="Accept=*/*")
public @ResponseBody ModelAndView oneFaultyMethod()
{
if(true)
{
throw new NullPointerException("This error message if for demo only.");
}
//Dead code
//Read more: http://howtodoinjava.com/2012/11/28/dead-code-and-unreachable-code-in-java-puzzle/
return null;
}
If we deploy above application and hit the URL [http ://localhost:8080/firstSpringApplication/users/demo/not-exist] in browser, it will show the error page as configured.
< %@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
< %@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>
< %@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
< %@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<head>
<title>This is sample error page</title>
</head>
<body>
<h1>This is sample error page : <c:out value="${message}"></c:out></h1>
</body>
Below will be the output in browser.
Handling multiple exceptions of different types
As mentioned earlier, above exception handler will handle all exceptions which are either instance of given class or sub-classes of it. But, if you want to configure multiple exceptions of different types, then you can specify all such exceptions in form of array.
@ExceptionHandler({NullPointerException.class, ArrayIndexOutOfBoundsException.class, IOException.class})
public ModelAndView handleException(NullPointerException ex)
{
//Do something additional if required
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("error");
modelAndView.addObject("message", ex.getMessage());
return modelAndView;
}
Download source code for this article
Recommended Reading : http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/ExceptionHandler.html
Happy Learning !!



Discussion
No comments yet.