예외처리

//ExceptionError1.java class ExceptionError1 { public static void main(String args[]) { try { System.out.println("매개변수로 받은 두 개의 값"); int a = Integer.parseInt(args[0]); // 문자열 값을 정수로 변환 int b = Integer.parseInt(args[1]); System.out.println(" a = " + a + " b = " + b ); System.out.println(" a를 b로 나눈 몫 = " + (a/b) ); System.out.println("나눗셈이 원할히 수행되었습니다."); } catch(ArithmeticException e) { System.out.println("=================================="); System.out.println("ArithmeticException 처리 루틴 : "); System.out.println(e + " 예외 발생"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("=================================="); System.out.println("ArrayIndexOutOfBoundsException 처리 루틴"); System.out.println(e + " 예외 발생"); } catch(NumberFormatException e) { System.out.println("=================================="); System.out.println("NumberFormatException 처리 루틴"); System.out.println(e + " 예외 발생"); } finally { System.out.println("=================================="); System.out.println("예외 처리를 끝내고 finally 블럭을 수행합니다"); } } } //ExceptionError2.java class ExceptionError2 { public static void main(String args[]) { try { System.out.println("매개변수로 받은 두 개의 값"); int a = Integer.parseInt(args[0]); // 문자열 값을 정수로 변환 int b = Integer.parseInt(args[1]); System.out.println(" a = " + a + " b = " + b ); System.out.println(" a를 b로 나눈 몫 = " + (a/b) ); System.out.println("나눗셈이 원할히 수행되었습니다."); } catch(Exception e) { System.out.println("=================================="); System.out.println("Exception 관련 모든 예외 처리 루틴 : "); System.out.println(e + " 예외 발생"); } finally { System.out.println("=================================="); System.out.println("예외 처리를 끝내고 finally 블럭을 수행합니다"); } } } //ExceptionSearch.java class ExceptionSearch { static int a, b; public static void main(String args[]) { try { a = Integer.parseInt(args[0]); // 문자열 값을 정수로 변환 b = Integer.parseInt(args[1]); System.out.println("매개변수로 입력받은 두 개의 값 :"); System.out.println(" a = " + a + " b = " + b ); System.out.println("=============================="); System.out.println("a() 메소드 호출 전"); a(); System.out.println("a() 메소드 호출 후"); } catch(Exception e) { System.out.println("Exception 처리 루틴 : "); System.out.println(e + " 예외 발생"); } finally { System.out.println("메소드 main()의 finally 블록 수행"); } } public static void a() { try { System.out.println("b() 메소드 호출 전"); b(); System.out.println("b() 메소드 호출 후"); } catch(ArithmeticException e) { System.out.println("ArithmeticException 처리 루틴 : "); System.out.println(e + " 예외 발생"); } finally { System.out.println("메소드 a()의 finally 블록 수행"); } } public static void b() { try { System.out.println("c() 메소드 호출 전"); c(); System.out.println("c() 메소드 호출 후"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException 처리 루틴 : "); System.out.println(e + " 예외 발생"); } finally { System.out.println("메소드 b()의 finally 블록 수행"); } } public static void c() { try { System.out.println(" a를 b로 나눈 몫 = " + (a/b) ); } catch(ClassCastException e) { System.out.println("ClassCastException 처리 루틴 : "); System.out.println(e + " 예외 발생"); } finally { System.out.println("=============================="); System.out.println("메소드 c()의 finally 블록 수행"); } } } //ExceptionGen.java class ExceptionGen { public static void main(String args[]) { try { a(); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("=================================="); System.out.println("ArrayIndexOutOfBoundsException 처리 루틴"); System.out.println(e + " 발생"); } } static void a() { try { throw new NullPointerException("널 포인터 예외"); // 예외의 인위적 발생 } catch(NullPointerException e) { System.out.println("=================================="); System.out.println("NullPointerException 처리 루틴"); System.out.println(e + " 발생"); throw new ArrayIndexOutOfBoundsException("배열 첨자 예외"); // 예외 처리 루틴에서 인위적으로 예외를 발생 시켜 호출한 메소드로 반환 } } } //ExceptionTrans.java class ExceptionTrans { static int a, b; public static void main(String args[]) { try { a = Integer.parseInt(args[0]); // 문자열 값을 정수로 변환 b = Integer.parseInt(args[1]); System.out.println("매개변수로 입력받은 두 개의 값 :"); System.out.println(" a = " + a + " b = " + b ); System.out.println("=============================="); System.out.println("a() 메소드 호출 전"); a(); System.out.println("a() 메소드 호출 후"); } catch(ArithmeticException e) { System.out.println("ArithmeticException 처리 루틴 : "); System.out.println(e + " 예외 발생"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException 처리 루틴 : "); System.out.println(e + " 예외 발생"); } catch(ClassCastException e) { System.out.println("ClassCastException 처리 루틴 : "); System.out.println(e + " 예외 발생"); } catch(Exception e) { System.out.println("나머지 모든 예외 처리 루틴 : "); System.out.println(e + " 예외 발생"); } finally { System.out.println("메소드 main()의 finally 블록 수행"); } } public static void a() throws ClassCastException, ArrayIndexOutOfBoundsException, ArithmeticException { System.out.println("b() 메소드 호출 전"); b(); System.out.println("b() 메소드 호출 후"); } public static void b() throws ClassCastException, ArrayIndexOutOfBoundsException { System.out.println("c() 메소드 호출 전"); c(); System.out.println("c() 메소드 호출 후"); } public static void c() throws ClassCastException { System.out.println("최종 메소드 예외 발생 문장 수행 전"); System.out.println("a를 b로 나눈 몫 = " + (a/b) ); System.out.println("최종 메소드 예외 발생 문장 수행 후"); } } //ExceptionByUser.java import java.util.*; class ExceptionByUser { public static void main(String args[]) { try { int a = Integer.parseInt(args[0]); if(a % 2 == 0) { throw new UserException1("사용자 정의 예외 1"); } else { throw new UserException2("사용자 정의 예외 2"); } } catch(UserException1 e) { System.out.println("UserException1 처리 루틴 : "); System.out.println(e + " 발생"); } catch(UserException2 e) { System.out.println("UserException2 처리 루틴 : "); System.out.println(e + " 발생"); } catch(Exception e) { System.out.println("모든 예외 처리 루틴 : "); System.out.println(e + " 발생"); } } } class UserException1 extends Exception { // 사용자 정의 예외는 Exception 클래스로부터 상속 public UserException1(String message) { super(message); } } class UserException2 extends Exception { public UserException2(String message) { super(message); } }