์์ธ์ฒ๋ฆฌ (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("์์ธ ๋ฐ์");
}
}
์์ธ์ฒ๋ฆฌ๋ฅผ ํ๊ธฐ ์ํ 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>
๋ธ๋ผ์ฐ์ ๋ก ์๋ฌ๊ฐ ์ถ๋ ฅ๋๋ ๊ฒ์ ํ์ธํ ์ ์๋ค
Author And Source
์ด ๋ฌธ์ ์ ๊ดํ์ฌ(์์ธ์ฒ๋ฆฌ (Exception Handler)), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค https://velog.io/@jooog/์์ธ์ฒ๋ฆฌ-Exception-Handler์ ์ ๊ท์: ์์์ ์ ๋ณด๊ฐ ์์์ URL์ ํฌํจ๋์ด ์์ผ๋ฉฐ ์ ์๊ถ์ ์์์ ์์ ์ ๋๋ค.
์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ
์ธ ๋ฐ๊ฒฌ์ ์ ๋
(Collection and Share based on the CC Protocol.)
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค