포워딩(전송)을 하게되면 오류가 발생할 수 있는데 이 오류를 두 가지로 나눌 수 있다. 컴파일오류와 실행오류이다. 컴파일오류는 오타를 쳤을때고 Java에서 알려준다.
실행오류는 예를 들어 무한 for문이다. 내가 생각한 결과물과 다른 결과물이 나올때다.
서버는 잘뜨나 실행이 되지 않는다.
다루어볼 것은 실행오류에 대한 처리이다. 오류는 시스템 오류와 예외가 있다. 시스템 오류는 잡을 수 없다.
예외는 내가 생각한 거와 다른 결과물로 프로그래밍으로 제어(제거)할 수가 있다.
모든 오류의 부모는 Throwable이다. 우리가 제어 할 수 있는 예외의 부모는 Exception이고, 우리가 제어할 수 없는 시스템 오류의 부모는 error이다.
실무에서 사용하는 ExceptionHandling이다.
Exception오류는 콘솔에 안 뜬다. 왜냐하면 자바컴파일러는 오류로 생각하지 않기 때문이다.
실행오류예외처리 (RuntimeException) try catch문을 사용해서 콘솔에 오류가 뜨게 만든다. try catch는 try구문 안에서 RuntimeException같은 종류의 오류가 발생하면 무조건 catch문을 타게된다.
++실무에서 운영서버나 test서버에 프로젝트를 올리게 되면 Java파일을 올리는 것이 아니라 class파일을 올리게 된다. 오류가 나게 되면 의지할 수 있는 곳은 콘솔 밖에 없다. 그러면 콘솔과 내 로컬 서버를 비교하면서 오류를 찾게 된다. 그렇기 때문에 어떠한 오류가 났는지 확인 하도록 값을 같이 찍어준다.
@Controller
public class WelcomeWebController {
@Resource
private WelcomeWebService welcomeWebService;
@RequestMapping(value = "/welcomeWeb.do")
public String welcomeWeb(ModelMap model) {
try {
//String nulls = null;
//System.out.println(nulls.toString());
List<EgovMap> welcomeWebList = welcomeWebService.selectWelcomeWebServiceList();
System.out.println(welcomeWebList);
//List를 Map에 담아 출력해보기
HashMap<String, Object> exceptionMap = new HashMap<String, Object>();
//여기서 오류냄
for (int i = 0; i < welcomeWebList.size()+2; i++) {
exceptionMap.put((String) welcomeWebList.get(i).get("seqNo"), welcomeWebList.get(i).getValue(1));
}
System.out.println("exceptionMap : " + exceptionMap);
//담은 Map의 데이터를 배열로 출력해보기
String[] arrStr = new String[exceptionMap.size()];
for (int i = 0; i < exceptionMap.size(); i++) {
arrStr[i] = (String) exceptionMap.get(String.valueOf(i+1));
}
System.out.println("arrStr : " + Arrays.toString(arrStr));
List<EgovMap> pieChartList = welcomeWebService.selectPieChartList();
System.out.println(pieChartList);
model.addAttribute("welcomeWebList", welcomeWebList);
model.addAttribute("pieChartList", pieChartList);
} catch (Exception e) {
System.out.println(e + "그 외 모든 오류들");
}
return "welcomeWeb/welcomeWeb.tiles";
}
}
보통 array오류와 nullpoint오류가 많이 난다. 팀프로젝트에서는 test서버에서는 확인하기 힘들어서 꼭 예외 처리해준다.
일단, try문에 String 변수에다가 null을 넣어준다.
@Controller
public class WelcomeWebController {
@Resource
private WelcomeWebService welcomeWebService;
@RequestMapping(value = "/welcomeWeb.do")
public String welcomeWeb(ModelMap model) {
try {
String nulls = null;
System.out.println(nulls.toString());
List<EgovMap> welcomeWebList = welcomeWebService.selectWelcomeWebServiceList();
System.out.println(welcomeWebList);
//List를 Map에 담아 출력해보기
HashMap<String, Object> exceptionMap = new HashMap<String, Object>();
for (int i = 0; i < welcomeWebList.size(); i++) {
exceptionMap.put((String) welcomeWebList.get(i).get("seqNo"), welcomeWebList.get(i).getValue(1));
}
System.out.println("exceptionMap : " + exceptionMap);
//담은 Map의 데이터를 배열로 출력해보기
String[] arrStr = new String[exceptionMap.size()];
for (int i = 0; i < exceptionMap.size()+2; i++) {
arrStr[i] = (String) exceptionMap.get(String.valueOf(i+1));
}
System.out.println("arrStr : " + Arrays.toString(arrStr));
List<EgovMap> pieChartList = welcomeWebService.selectPieChartList();
System.out.println(pieChartList);
model.addAttribute("welcomeWebList", welcomeWebList);
model.addAttribute("pieChartList", pieChartList);
} catch (ArrayIndexOutOfBoundsException ae) {
System.out.println(ae + "배열의 길이를 확인해!");
} catch (NullPointerException ne) {
System.out.println(ne + "null있다.");
return "welcomeWeb/welcomeWeb.tiles";
}
}
변수를 참조해서 실행하는데 우리가 변수에 null을 했기 때문에 참조할 수 없다. 그래서 콘솔에 nullpoint오류가 난다.
여러 오류가 발생한다면, 앞에 오류가 나오면 그 뒤에 오류는 안나온다.
그래서 무슨 오류가 나오는지 여러 오류처리를 해야한다. 어디 controller에서 나온 오류인지 확인할 수 있도록 controller명도 같이 찍어준다.
@Controller
public class WelcomeWebController {
@Resource
private WelcomeWebService welcomeWebService;
@RequestMapping(value = "/welcomeWeb.do")
public String welcomeWeb(ModelMap model) throws Exception{
try {
String nulls = null;
System.out.println(nulls.toString());
List<EgovMap> welcomeWebList = welcomeWebService.selectWelcomeWebServiceList();
System.out.println(welcomeWebList);
//List를 Map에 담아 출력해보기
HashMap<String, Object> exceptionMap = new HashMap<String, Object>();
for (int i = 0; i < welcomeWebList.size(); i++) {
exceptionMap.put((String) welcomeWebList.get(i).get("seqNo"), welcomeWebList.get(i).getValue(1));
}
System.out.println("exceptionMap : " + exceptionMap);
//담은 Map의 데이터를 배열로 출력해보기
String[] arrStr = new String[exceptionMap.size()];
for (int i = 0; i < exceptionMap.size()+2; i++) {
arrStr[i] = (String) exceptionMap.get(String.valueOf(i+1));
}
System.out.println("arrStr : " + Arrays.toString(arrStr));
List<EgovMap> pieChartList = welcomeWebService.selectPieChartList();
System.out.println(pieChartList);
model.addAttribute("welcomeWebList", welcomeWebList);
model.addAttribute("pieChartList", pieChartList);
} catch (ArrayIndexOutOfBoundsException ae) {
System.out.println(ae + "배열의 길이를 확인해!");
System.out.println("welcomeWeb Controller에서 오류");
} catch (NullPointerException ne) {
System.out.println(ne + "null있다.");
System.out.println("welcomeWeb Controller에서 오류");
}
return "welcomeWeb/welcomeWeb.tiles";
}
}
오류가 나던 안 나던 무조건 타게 만드는 메서드는 finally이다. try catch문에서만 실행이 된다.
@Controller
public class WelcomeWebController {
@Resource
private WelcomeWebService welcomeWebService;
@RequestMapping(value = "/welcomeWeb.do")
public String welcomeWeb(ModelMap model) throws Exception{
try {
String nulls = null;
System.out.println(nulls.toString());
List<EgovMap> welcomeWebList = welcomeWebService.selectWelcomeWebServiceList();
System.out.println(welcomeWebList);
//List를 Map에 담아 출력해보기
HashMap<String, Object> exceptionMap = new HashMap<String, Object>();
for (int i = 0; i < welcomeWebList.size(); i++) {
exceptionMap.put((String) welcomeWebList.get(i).get("seqNo"), welcomeWebList.get(i).getValue(1));
}
System.out.println("exceptionMap : " + exceptionMap);
//담은 Map의 데이터를 배열로 출력해보기
String[] arrStr = new String[exceptionMap.size()];
for (int i = 0; i < exceptionMap.size()+2; i++) {
arrStr[i] = (String) exceptionMap.get(String.valueOf(i+1));
}
System.out.println("arrStr : " + Arrays.toString(arrStr));
List<EgovMap> pieChartList = welcomeWebService.selectPieChartList();
System.out.println(pieChartList);
model.addAttribute("welcomeWebList", welcomeWebList);
model.addAttribute("pieChartList", pieChartList);
} catch (ArrayIndexOutOfBoundsException ae) {
System.out.println(ae + "배열의 길이를 확인해!");
} catch (NullPointerException ne) {
System.out.println(ne + "null있다.");
} finally {
System.out.println("welcomeWeb Controller에서 오류");
System.out.println("오류가 나던 안 나던 출력");
}
return "welcomeWeb/welcomeWeb.tiles";
}
}
모든 실행오류를 적어줄 수 없지만 어떤 오류인지 알수 없기 때문에 최상위 Exception를 적어준다. 이 최상위를 위에 적어주면 나머지 오류는 실행되지 않기 때문에 최상위 Exception을 맨 아래에 넣는다.
이 오류는 꼭 있어야 하는 처리문이다.
@Controller
public class WelcomeWebController {
@Resource
private WelcomeWebService welcomeWebService;
@RequestMapping(value = "/welcomeWeb.do")
public String welcomeWeb(ModelMap model) throws Exception{
try {
String nulls = null;
System.out.println(nulls.toString());
List<EgovMap> welcomeWebList = welcomeWebService.selectWelcomeWebServiceList();
System.out.println(welcomeWebList);
//List를 Map에 담아 출력해보기
HashMap<String, Object> exceptionMap = new HashMap<String, Object>();
for (int i = 0; i < welcomeWebList.size(); i++) {
exceptionMap.put((String) welcomeWebList.get(i).get("seqNo"), welcomeWebList.get(i).getValue(1));
}
System.out.println("exceptionMap : " + exceptionMap);
//담은 Map의 데이터를 배열로 출력해보기
String[] arrStr = new String[exceptionMap.size()];
for (int i = 0; i < exceptionMap.size()+2; i++) {
arrStr[i] = (String) exceptionMap.get(String.valueOf(i+1));
}
System.out.println("arrStr : " + Arrays.toString(arrStr));
List<EgovMap> pieChartList = welcomeWebService.selectPieChartList();
System.out.println(pieChartList);
model.addAttribute("welcomeWebList", welcomeWebList);
model.addAttribute("pieChartList", pieChartList);
} catch (ArrayIndexOutOfBoundsException ae) {
System.out.println(ae + "배열의 길이를 확인해!");
} catch (NullPointerException ne) {
System.out.println(ne + "null있다.");
} catch (Exception e) {
System.out.println(e + "그 외 모든 오류들");
} finally {
System.out.println("welcomeWeb Controller에서 오류");
System.out.println("오류가 나던 안 나던 출력");
}
return "welcomeWeb/welcomeWeb.tiles";
}
}
try catch는 내가 책임지겠다는 것이고 throws exception는 나를 호출한 놈한테 책임을 주는 것이다. 책임회피다. try랑 throw를 같이 적어주면 안된다. controller에는 throw exception을 적어주지 않는 것을 추천한다.
throw Exception의 controller에 적어주면 Java 뒷단에 책임을 지게 한다.