Exception
์
๋๋ณด๊ธฐ
// ExceptionController.java
@Controller
public class ExceptionController {
@ExceptionHandler(Exception.class) // ex
public String catcher(Exception ex, Model m) {
System.out.println("catcher() in ExceptionController");
System.out.println("m="+m);
m.addAttribute("ex", ex);
return "error";
}
@ExceptionHandler({NullPointerException.class, FileNotFoundException.class}) // ex2
public String catcher2(Exception ex, Model m) {
m.addAttribute("ex", ex);
return "error";
}
@RequestMapping("/ex") // ์๋ฌ ๋ฐ์ ํ ํธ๋ค๋ฌ ์ฒ๋ฆฌ
public String main(Model m) throws Exception {
m.addAttribute("msg","message from ExceptionController.main()");
throw new Exception("์์ธ๊ฐ ๋ฐ์ํ์ต๋๋ค.");
}
@RequestMapping("/ex2")
public String main2() throws Exception {
throw new FileNotFoundException("์์ธ๊ฐ ๋ฐ์ํ์ต๋๋ค.");
}
}
@ExceptionHandler
Exception Method
์์ธ๊ฐ ๋ฐ์ํ ์๋ฌ๋ฅผ ์ฒ๋ฆฌ
@ExceptionHandler ๋ฅผ ์ฌ์ฉํ๊ณ ํด๋น Exception.class ๋ฅผ ์ฌ์ฉํ๋ค.
{ } ์ค๊ดํธ๋ฅผ ํตํด ์ฌ๋ฌ Exception.class ๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
// ExceptionController.java
@ExceptionHandler(Exception.class) // ex
public String catcher(Exception ex, Model m) {
m.addAttribute("ex", ex);
return "error";
}
@ExceptionHandler({NullPointerException.class, FileNotFoundException.class}) // ex2
public String catcher2(Exception ex, Model m) {
m.addAttribute("ex", ex);
return "error";
}
Exception Message
์์ธ๊ฐ ๋ฐ์ํ์ ๋ ๊ฒฝ๋ก ์ค์
// ExceptionController.java
@RequestMapping("/ex") // ์๋ฌ ๋ฐ์ ํ ํธ๋ค๋ฌ ์ฒ๋ฆฌ
public String main() throws Exception {
throw new Exception("์์ธ๊ฐ ๋ฐ์ํ์ต๋๋ค.");
}
@RequestMapping("/ex2")
public String main2() throws Exception {
throw new FileNotFoundException("์์ธ๊ฐ ๋ฐ์ํ์ต๋๋ค.");
}
@ControllerAdvice
์ ์ญ ์์ธ ์ฒ๋ฆฌ ํด๋์ค ์์ฑ ๊ฐ๋ฅ(ํจํค์ง ์ง์ ๊ฐ๋ฅ)
์์ธ ์ฒ๋ฆฌ ๋ฉ์๋๊ฐ ์ค๋ณต๋ ๊ฒฝ์ฐ, ์ปจํธ๋กค๋ฌ ๋ด์ ์์ธ ์ฒ๋ฆฌ ๋ฉ์๋๊ฐ ์ฐ์
// GlobalCatcher.java
@ControllerAdvice("com.ldh.ch2") // ์ง์ ๋ ํจํค์ง์์ ๋ฐ์ํ ์์ธ๋ง ์ฒ๋ฆฌ
// @ControllerAdvice // ๋ชจ๋ ํจํค์ง ์ ์ฉ
public class GlobalCatcher {
@ExceptionHandler(Exception.class) // ex
public String catcher(Exception ex, Model m) {
m.addAttribute("ex", ex);
return "error";
}
@ExceptionHandler({NullPointerException.class, FileNotFoundException.class}) // ex2
public String catcher2(Exception ex, Model m) {
m.addAttribute("ex", ex);
return "error";
}
}
๋ค๋ฅธ ์์ธ์ฒ๋ฆฌ ์ปจํธ๋กค๋ฌ์์ ์์ธ์ฒ๋ฆฌ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ์ ์ญ ์์ธ์ฒ๋ฆฌ๋ฅผ ์ํด GlobalCatcher ๋ฅผ ๋ง๋ค์ด ํ ๋ฒ์ ์ฒ๋ฆฌํ ์ ์๋ค.
// ExceptionController2.java
@Controller
public class ExceptionController2 {
@RequestMapping("/ex3") // ์์ธ์ฒ๋ฆฌ ๋ฉ์๋๊ฐ ์์ด๋ GlobalCatcher ์ปจํธ๋กค๋ฌ์์ ์์ธ์ฒ๋ฆฌ
public String main() throws Exception {
throw new Exception("์์ธ๊ฐ ๋ฐ์ํ์ต๋๋ค.");
}
@RequestMapping("/ex4") // ์์ธ์ฒ๋ฆฌ ๋ฉ์๋๊ฐ ์์ด๋ GlobalCatcher ์ปจํธ๋กค๋ฌ์์ ์์ธ์ฒ๋ฆฌ
public String main2() throws Exception {
throw new FileNotFoundException("์์ธ๊ฐ ๋ฐ์ํ์ต๋๋ค.");
}
}
@ResponseStatus
์๋ต ๋ฉ์ธ์ง์ ์ํ ์ฝ๋๋ฅผ ๋ณ๊ฒฝํ ๋ ์ฌ์ฉ
Default 500 ์ฝ๋ ์๋ฌ
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) // 405 ์๋ฌ
@ExceptionHandler({NullPointerException.class, FileNotFoundException.class}) // ex2
public String catcher2(Exception ex, Model m) {
m.addAttribute("ex", ex);
return "error";
}
์ถ์ฒ | ํจ์คํธ ์บ ํผ์ค