객체지향 프로그래밍을 위한 내용은 아니다. 프로그래밍을 위한 에러 처리에 대한 전반적인 내용이다.
: 프로그램을 실행하다 보면 어떤 원인 때문에 비정상적인 동작을 일으켜 프로그램이 종료되는 상황이다. 이를 오류가 발생했다고 말한다.
+컴파일 오류 (잘못된 코드 부분을 캐치하여 미리 빨간 줄로 오류 보여줌.)

int n1 = 10, n2 = 0;
System.out.println("나눗셈 시작!");
int result = n1 / n2; // 10/0 ? 오류날만한 부분인 곳 !
System.out.printf("%d / %d = %d", n1, n2, result);
System.out.println("나눗셈 정상 종료 !");
// 에러가 날 부분에 예외 발생 가능성이 있는 코드 부분을 예측 !!
// try { 튕길 거 같은 부분 } catch () { 튕기는 대신 실행될 코드 }
try {
int result = n1 / n2; // 10/0 ? 오류날만한 부분인 곳 !
System.out.printf("%d / %d = %d", n1, n2, result);
} catch (Exception e) {
// 예외가 발생하여 프로그램이 튕기는 대신 실행할 코드 작성
System.out.println("0으로 나누면 안됩니다 ~");
}
// 예외 처리날 부분에 ctrl + alt + t = try/catch
try {
System.out.print("정수1 : ");
int n1 = Integer.parseInt(sc.next()); // NumberFormatException
System.out.print("정수2 : ");
int n2 = sc.nextInt(); // InputMismatchException
int result = n1 / n2; // ArithmeticException
System.out.println("result = " + result);
} catch (Exception e) {
System.out.println("정수를 제대로 입력하세요.");
}
System.out.println("프로그램 정상 종료 !");