1 class Error public static void mainString args
1. 예외의 개요 예외가 발생되는 예 class Error { public static void main(String[] args) { 에러 메시지 x(); // x() 메소드 호출 java. lang. Arithmetic. Exception: / by zero } at Error. z(Error. java: 14) static void x() { at Error. y(Error. java: 9) at Error. x(Error. java: 6) y(); // y() 메소드 호출 at Error. main(Error. java: 3) } static void y() { z(); // z() 메소드 호출 } static void z() { int i = 1; int j = 0; System. out. println(i/j); // 1을 0으로 나눈다. 예외 발생 } // JVM의 묵시적인 예외 처리기 가동 } Chap 09 예외 처리
2. 예외 관련 클래스 (프로그램에서 처리하지 않고, JVM에게 예외처리를 맡긴다) Linkage. Error Object Error Throwable Thread. Death Arithmetic. Exception Negative. Array. Size. Exception Virtual. Machine. Error Null. Point. Exception Runtime. Exception Array. Store. Exception Clone. Not. Supported. Exception Illegal. Access. Exception Class. Cast. Exception Instantiation. Exception Index. Out. Of. Bound. Exception (Array. Index. Out. Of. Bounds. Exception String. Index. Out. Of. Bounds. Exception) Security. Exception Interrupted. Exception No. Such. Method. Exception Class. Not. Found. Exception IOException 예외 클래스 계층구조 EOFException File. Not. Found. Exception Interrupted. IOException Chap 09 예외 처리
3. 예외 처리 예외의 발생 import java. io. *; class IOException. Error { public static void main(String args[]) { File. Reader file = new File. Reader("a. txt"); // 만일 a. txt 파일이 없다면? int i; while((i = file. read()) != -1) { System. out. print((char)i); } 이 프로그램은 컴파일러에 의해 에러가 발생된다. file. close(); 즉 컴파일러는 a. txt 파일이 없을 경우에 발생하는 예외의 } 처리를 요구한다 } Chap 09 예외 처리
3. 예외 처리 • 컴파일러는 앞의 프로그램에 대해 다음과 같은 에러 메시지를 나타낸 다 IOException. Error. java: 7: Exception java. io. File. Not. Found. Exception must be caught, or it must be declared in the throws clause of this method. File. Reader file = new File. Reader("a. txt"); ^ IOException. Error. java: 11: Exception java. io. IOException must be caught, or it must be declared in the throws clause of this method. while((i = file. read()) != -1) ^ 2 errors Chap 09 예외 처리
3. 예외 처리 - try, catch, finally 블록 사용 예 import java. io. *; class IOException. Error { public static void main(String args[]) { try { // 예외가 발생할 가능성이 있는 문장을 try 블록으로 지정 File. Reader file = new File. Reader("a. txt"); int i; while((i = file. read()) != -1) { System. out. print((char)i); } file. close(); } catch(Exception e) { // 발생된 예외를 처리하는 부분 System. out. println("예외 처리 루틴 : " + e + " 예외 발생"); } } } Chap 09 예외 처리
3. 예외 처리 예제 1 : Exception. Error 1. java 예제 2 : Exception. Error 2. java 예제 3 : Exception. Search. java Chap 09 예외 처리
- Slides: 17