異常処理(Exception Handler)


🎈例外の処理

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("예외 발생");
    }

}
Exception Controlを作成して例外処理を行い、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メソッドの作成とコードの再パッケージ
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 blockを使用して例外処理を行った場合、再構築されたコードは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("예외 발생");
    }
}
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>

ブラウザでエラー出力を表示できます