nme.kr

문서의 이전 판입니다!


Chapter11 예외처리

11.3 예외처리

try ~catch문의 구조

try {
    // 예외가 발생할 가능성이 있는 문장 코드
} catch (Exception1 e1) {
    // Exception1이 발생할 경우, 실행될 문장
} catch (Exception1 e2) {
    // Exception2이 발생할 경우, 실행될 문장
...
} catch (ExceptionX eX){
    // ExceptionX가 발생할 경우, 실행될 문장
}

package chapter11;

public class ExceptionEx0 {

	public static void main(String[] args) {
		System.out.println(1);
		System.out.println(2);
		System.out.println(3/0);
		System.out.println(4);
		System.out.println(5);
		System.out.println(6);
	}
	
}

package chapter11;

public class ExceptionEx {

	public static void main(String[] args) {
		System.out.println(1);
		try {
			System.out.println(2);
			System.out.println(3/0);
			System.out.println(4);
		} catch(ArithmeticException e) {
			System.out.println(5);
		}
		System.out.println(6);
	}
	
}

11.4 예외 강제 발생

11.5 예외 떠넘기기

11.6 사용자 정의 예외 클래스