๐Ÿ’ป ๊ณต๋ถ€ ๊ธฐ๋ก/๐Ÿƒ Spring

Spring | Exception

  • -

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";
}

 

 

 

์ถœ์ฒ˜ | ํŒจ์ŠคํŠธ ์บ ํผ์Šค

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.