์˜ˆ์™ธ์ฒ˜๋ฆฌ (Exception Handler)

28002 ๋‹จ์–ด SpringSpring

๐ŸŽˆ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ•˜๊ธฐ

package hello.hellospring;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ExceptionController {

    @RequestMapping("/ex")
    public void main() throws Exception{
        throw new Exception("์˜ˆ์™ธ ๋ฐœ์ƒ");
    }

}

์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•œ ExceptionController๋ฅผ ๋งŒ๋“ค๊ณ  localhost:8080/ex๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

๐ŸŽˆExceptionController

controller

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ExceptionController {

    @RequestMapping("/ex")
    public String main() throws Exception{
        try {
            throw new Exception("์˜ˆ์™ธ ๋ฐœ์ƒ");
        } catch(Exception e) {
            return "error";
        }
    }

}

ํƒ€์ž„๋ฆฌํ”„

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Error</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1 th:text="'์—๋Ÿฌ ๋ฐœ์ƒ'" ></h1>
</body>
</html>

localhost:8080/ex๋กœ ์š”์ฒญํ•˜๋ฉด ์—๋ŸฌํŽ˜์ด์ง€๊ฐ€ ๋œฌ๋‹ค

Exception method ๋งŒ๋“ค์–ด ์ฝ”๋“œ ๋ฆฌํŒฉํ† ๋ง ํ•ด๋ณด๊ธฐ

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

@Controller
public class ExceptionController {

    @ExceptionHandler(Exception.class)
    public String catcher(Exception e){
        return "error";
    }

    @RequestMapping("/ex")
    public String main() throws Exception{
            throw new Exception("์˜ˆ์™ธ ๋ฐœ์ƒ");
    }

    @RequestMapping("/ex2")
    public String main2() throws Exception{
        throw new Exception("์˜ˆ์™ธ ๋ฐœ์ƒ");
    }
}

์ด์ „ ์ฝ”๋“œ์—์„œ try, catcher ๋ธ”๋Ÿญ์„ ์‚ฌ์šฉํ•ด์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ–ˆ๋‹ค๋ฉด ๋ฆฌํŒฉํ† ๋งํ•œ ์ฝ”๋“œ๋Š” Exception method๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ–ˆ๋‹ค.

NullPointerException ์ถ”๊ฐ€ํ•ด์„œ ๋ฆฌํŒฉํ† ๋ง

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

@Controller
public class ExceptionController {

    @ExceptionHandler(NullPointerException.class)
    public String catcher2(Exception e){
        return "error";
    }

    @ExceptionHandler(Exception.class)
    public String catcher(Exception e){
        return "error";
    }

    @RequestMapping("/ex")
    public String main() throws Exception{
            throw new Exception("์˜ˆ์™ธ ๋ฐœ์ƒ");
    }

    @RequestMapping("/ex2")
    public String main2() throws Exception{
        throw new NullPointerException("์˜ˆ์™ธ ๋ฐœ์ƒ");
    }
}

๋ทฐ ํ…œํ”Œ๋ฆฟ์—์„œ ์—๋Ÿฌ ์ถœ๋ ฅํ•˜๊ธฐ

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ExceptionController {

    @ExceptionHandler(NullPointerException.class)
    public String catcher2(Exception error, Model model){
        model.addAttribute("error", error);
        return "error";
    }

    @ExceptionHandler(Exception.class)
    public String catcher(Exception error, Model model){
        model.addAttribute("error", error);
        return "error";
    }

    @RequestMapping("/ex")
    public String main() throws Exception{
            throw new Exception("์˜ˆ์™ธ ๋ฐœ์ƒ");
    }

    @RequestMapping("/ex2")
    public String main2() throws Exception{
        throw new NullPointerException("์˜ˆ์™ธ ๋ฐœ์ƒ");
    }
}

model์— error ๊ฐ์ฒด ๋‹ด์•„ ์ „๋‹ฌ

 @ExceptionHandler(NullPointerException.class)
    public String catcher2(Exception error, Model model){
        model.addAttribute("error", error);
        return "error";
    }
 
 @ExceptionHandler(Exception.class)
    public String catcher(Exception error, Model model){
        model.addAttribute("error", error);
        return "error";
    }

ํƒ€์ž„๋ฆฌํ”„

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Error</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1 th:text="${error}" ></h1>
</body>
</html>

๋ธŒ๋ผ์šฐ์ €๋กœ ์—๋Ÿฌ๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ