Java
예외처리, try~catch
GOD03219
2018. 1. 9. 10:52
public class Day3 {
public static void main(String[] args) {
try {
int a = 3 / 0;
} catch (Exception e) {
System.out.println("error"); // try에서 에러가 나면 catch문으로 넘어온다.
e.printStackTrace(); // 지금 어떤 exception이 발생 했는 지 출력해준다.
}
class Apple {
void func01() {
try {
throw new Exception(); // throw->exception을 발생시킴 ,throw는 반드시 try/catch문이 받아야함!!
} catch (Exception e) {
}
}
void func02() throws Exception {
throw new Exception();
}
}
Apple apple = new Apple();
apple.func01();
// apple.func02(); // Unhandled exception -> func02를 호출할 때 tryCatch를 책임지라! Ctrl+1 > Surround with try/catch
try {
apple.func02();
} catch (Exception e) {
}
}
}