Skip to content

Instantly share code, notes, and snippets.

@bmchild
Created August 30, 2012 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmchild/3527471 to your computer and use it in GitHub Desktop.
Save bmchild/3527471 to your computer and use it in GitHub Desktop.
Example Spring MVC 3 Exception Handlers
@Controller
public class SimpleController {
// other controller method omitted
@ExceptionHandler(IOException.class)
public String handleIOException(IOException ex, HttpServletRequest request) {
return ClassUtils.getShortName(ex.getClass());
}
}
@Controller
public class SimpleController {
// other controller method omitted
@ResponseStatus(value=HttpStatus.FORBIDDEN)
@ExceptionHandler(SecurityException.class)
public ModelAndView workflowExceptionCaught(SecurityException ex) {
return new ModelAndView("common/_error", "errorCode", HttpStatus.FORBIDDEN.value());
}
}
@Controller
public class SimpleController {
// other controller method omitted
@ExceptionHandler({Exception.class, IOException.class})
public ModelAndView exceptionHandler(Exception e) {
LOGGER.error("An error has occurred - " + e.getMessage(), e);
ModelAndView mav = new ModelAndView("exception");
mav.addObject("showTrace", false);
mav.addObject("exception", e);
return mav;
}
}
<%-- includes omitted --%>
<div>
<div id="message" class="errorLabel">Exception Thrown: ${exception.message}</div>
<c:if test="${showTrace eq null || showTrace eq true }">
<div id="stackTrace">${exception.stackTrace}</div>
</c:if>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment