문제생기는 부분으로 인해 작업이 끊기게 하지 않고 에러부분을 확인할 수 있도록 해주는 이점이 있다
부모님의 이름으로 자식을 받을 수 있게 하는 것이 다형성이고 다형성이 있기 때문에 Exception e로 인자값을 받아 e.printStackTrace();해보면 에러메세지를 받을 수 있다.
ArithmeticException e와 같은 시스템 함수는 자바스크립트에서의 콜백함수와 같다.
package exceptionpkg;
public class ExceptionEx05 {
public static void main(String[] args) {
System.out.println(1);
System.out.println(2);
try {
System.out.println(3);
System.out.println(3/0); // 에러가 뜨는 순간 catch문으로 넘어가게됨
System.out.println(4);
} catch (ArithmeticException ae) {
System.out.println("ArithmeticException");
} catch (NullPointerException e) {
System.out.println("NullPointerException");
} catch (Exception e) {
// 아래로 결과값을 받아볼 수 있음
System.out.println(e.getMessage());
e.printStackTrace();
// 예외 발생 여부와 상관없이 수행
}finally {
System.out.println("end");
}
}
}

package exceptionpkg;
public class ExceptionEx06 {
// throws로 에러를 직접받아서 처리할 수 있음
public void exceptionMethod() throws Exception {
throw new Exception();
}
public static void main(String[] args) {
ExceptionEx06 ex6 = new ExceptionEx06();
try {
ex6.exceptionMethod();
} catch (Exception e) {
// 매개변수로 받은 e갑으로 오류 메세지를 받음
e.printStackTrace();
}
}
}

package exceptionpkg;
import java.util.Scanner;
public class LoginExceptionMain {
static final String USER_ID = "park";
static final String USER_PW = "1234";
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
try {
System.out.println("ID > ");
String inputId= sc.next();
System.out.println("PW > ");
String inputPW= sc.next();
if(!USER_ID.equals(inputId)) {
throw new LoginException("아이디가 틀립니다.");
}else if (!USER_PW.equals(inputPW)) {
throw new LoginException("비밀번호가 틀립니다.");
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
