문서의 이전 판입니다!
try { // 예외가 발생할 가능성이 있는 문장들을 넣는다. } catch (Exception1 e1) { // Exception1이 발생했을 경우, 이를 처리하기 위한 문장을 적는다. } catch (Exception2 e2) { // Exception2이 발생했을 경우, 이를 처리하기 위한 문장을 적는다. ... } catch (ExceptionN eN) { // ExceptionN이 발생했을 경우, 이를 처리하기 위한 문장을 적는다. }
☛ if 문과 달리 try블럭이나 catch블럭 내에 포함된 문장이 하나라고 해서 괄호{}를 생략할 수는 없다.
class ExceptionEx1 { public static void main(String args) { try { try { } catch (Exception e) { } } catch (Exception e) { try { } catch (Exception e) { } // 컴파일 에러 발생 !!! } // try-catch 의 끝 try { } catch (Exception e) { } // try-catch 의 끝 // main메서드의 끝 } }
catch블럭 내에 또 하나의 try-catch문이 포함된 경우, 같은 이름의 참조변수를 사용해서는 안된다. 각 catch블럭에 선언된 두 참조변수의 영역이 서로 겹치기 때문에 다른 이름을 사용해서 구별해야하기 때문이다.
class ExceptionEx2 { public static void main(String[] args) { int number = 100; int result = 0; for( int i = 0 ; i < 10 ; ++i ) { result = number / (int)(Math.random() 8 10); // 7번째 라인, 예외 발생 System.out.println(result); } } }
위 프로그램을 실행하면, 7번째 라인에서 예외가 발생하며 비정상적으로 멈출것이다. 또한, 어떤 예외가 발생했는지도 화면에 나타난다.
class ExceptionEx3 { public static void main(String[] args) { int number = 100; int result = 0; for( int i = 0 ; i < 10 ; ++i ) { try { result = number / (int)(Math.random() 8 10); // 7번째 라인, 예외 발생 System.out.println(result); } catch (ArithmeticException e) { System.out.println("0"); // ArithmeticException이 발생하면 실행되는 코드 } // try-catch 의 끝 } // for 의 끝 } }
class ExceptionEx4 { public static void main(String[] args) { System.out.println(1); System.out.println(2); try { System.out.println(3); System.out.println(4); } catch (Exception e) { System.out.println(5); } // try-catch의 끝 System.out.println(6); // main메서드의 끝 } }
위의 예제에서는 예외가 발생하지 않았으므로, catch블럭 문장이 실행되지 않는다.
class ExceptionEx5 { public static void main(String[] args) { System.out.println(1); System.out.println(2); try { System.out.println(3); System.out.println(0/0); // 0으로 나누어서 고의로 ArithmeticException을 발생시킨다. System.out.println(4); // 실행되지 않는다. } catch (ArithmeticException ae) { System.out.println(5); } // try-catch의 끝 System.out.println(6); // main메서드의 끝 } }
Object --- Throwable -+- Error -+- OutOfMemoryError | | | +- ... | + Exception -+- IOException | +- ... | +- RuntimeException
Exception -+- IOException | +- ClassNotFoundException | +- ... | +- RuntimeException -+- ArithmeticException | +- ClassCastException | +- NullPointException | +- ... | +- IndexOutBoundsException
class ExceptionEx8 { public static void main(String[] args) { throw new RuntimeException(); // RuntimeException을 강제로 발생시킨다. } }
throw e;
class ExceptionEx9 { public static void main(String[] args) { try { Exception e = new Exception("고의로 발생시켰음."); throw e; // 예외를 발생시킴. // throw new Exception("고의로 발생시켰음."); } catch (Exception e) { System.out.println("에러 메시지 : " + e.getMessage()); e.printStackTrace(); } System.out.println("프로그램이 정상 종료되었음."); } }
instanceof
연산자를 이용해서 검사하게 되는데, 검사결과가 true인 catch블럭을 만날 때까지 검사는 계속된다.class ExceptionEx10 { public static void main(String[] args) { System.out.println(1); System.out.println(2); try { System.out.println(3); System.out.println(0/0); // 0으로 나누어서 ArithmeticException을 발생시킨다. System.out.println(4); // 실행되지 않는다. } catch (Exception e) { // ArithmeticException대신 Exception을 사용. System.out.println(5); } System.out.println(6); } }
class ExceptionEx11 { public static void main(String[] args) { System.out.println(1); System.out.println(2); try { System.out.println(3); System.out.println(0/0); // 0으로 나누어서 ArithmeticException을 발생시킨다. System.out.println(4); // 실행되지 않는다. } catch (ArithmeticException ae) { if(ae instanceof ArithmeticException) Ssystem.out.println("true"); System.out.println("ArithmeticException"); } catch (Exception e) { // ArithmeticException을 제외한 모든 예외가 처리된다. System.out.println(5); } System.out.println(6); } }
class ExceptionEx12 { public static void main(String[] args) { System.out.println(1); System.out.println(2); try { System.out.println(3); System.out.println(0/0); // 예외발생!!! System.out.println(4); // 실행되지 않는다. } catch (ArithmeticException ae) { ae.printStackTrace(); // 참조변수 ae를 통해, 생성된 ArithmeticException인스턴스에 접근할 수 있다. System.out.println("예외메시지 : " + ae.getMessage()); } System.out.println(6); } }
위에 언급된 메서드들을 통해서 예외의 발생원인을 알 수 있다.
import java.io.*; class ExceptionEx13 { public static void main(String[] args) { PrintStream ps = null; FileOutputStream fos = null; try { fos = new FileOutputStream("error.log"); ps = new PrintStream(fos); System.out.println(1); System.out.println(2); System.out.println(3); System.out.println(0/0); // 예외발생!!! System.out.println(4); // 실행되지 않는다. } catch (ArithmeticException ae) { ae.printStackTrace(ps); // 인자로 ae를 넘겨주었다. ps.println("예외메시지 : " + ae.getMessage()); // 화면대신 error.log파일에 출력한다. System.out.println("예외가 발생하였으니 error.log 파일을 확인하시오."); } System.out.println(6); } }