Close

Spring MVC - @ExceptionHandler Examples

[Last Updated: Nov 8, 2025]

Spring MVC 


The annotation @ExceptionHandler can be used on methods of @Controller classes. One of these methods is invoked when an specified exception thrown by a @RequestMapping method. The method can return a view name or a message body by using @ResponseBody.


package com.logicbig.example;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import jakarta.servlet.http.HttpServletRequest;
import java.time.Year;

@Controller
public class ExampleController {

@RequestMapping("/data/{year}")
@ResponseBody
public String handleRequest(@PathVariable("year") int year) throws Exception {
if (Year.of(year).isBefore(Year.of(1990))) {
throw new Exception("Year is before 1990: " + year);
}
return "data response " + year;
}

@RequestMapping("/test/{id}")
@ResponseBody
public String handleRequest2(@PathVariable("id") String id) {
int i = Integer.parseInt(id);
return "test response " + i;
}

@RequestMapping("/admin")
@ResponseBody
public String handleRequest3(HttpServletRequest request)
throws UserNotLoggedInException {

Object user = request.getSession()
.getAttribute("user");
if (user == null) {
throw new UserNotLoggedInException("user: " + user);
}
return "test response " + user;
}

@ExceptionHandler(NumberFormatException.class)
public ModelAndView exceptionHandler(NumberFormatException re) {
ModelAndView mav = new ModelAndView("errorPage");
mav.addObject("exception", re);
return mav;
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(Exception.class)
@ResponseBody
public String exceptionHandler2(Exception re) {
return "Exception: " + re.getMessage();
}

@ResponseStatus(HttpStatus.FORBIDDEN)
@ExceptionHandler(UserNotLoggedInException.class)
public ModelAndView exceptionHandler3(UserNotLoggedInException e) {
ModelAndView mav = new ModelAndView("errorPage");
mav.addObject("exception", e);
return mav;
}
}
Original Post




package com.logicbig.example;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ExampleController {

/**
* This will throw MissingPathVariableException with response code 500
*/
@RequestMapping("/test/{id}")
@ResponseBody
public String handleRequest (@PathVariable("testId") String id) {
return "id: "+id;
}

/**
* The exception should be processed by @ExceptionHandler method
*/
@RequestMapping("/test")
public String handleRequest2 () throws Exception {
throw new IllegalStateException();
}


@ExceptionHandler
@ResponseBody
public String handleException (IllegalStateException b) {
return "from @ExceptionHandler method: " + b ;
}
}
Original Post




See Also

Join