day19_ExceptionEx7

육희영·2021년 10월 28일
0
package com.java1.day19;

/* 아래 예제에서는 
 * ArithmeticException 이 발생하는 예제이다.
 * 첫번째 검사에서 일치하는 블럭을 찾았기 때문에 두번째 catch 블럭은 검사하지 않는다.
 * 범위가 작은 예외를 첫catch로 하고 맨 마지감을 범위가 가장큰 Exception 으로 하도록 하자.
 */
public class ExceptionEx7 {
	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) {	// 여기서 예외가 걸리게 되면 아래 쪽 catch문을 처리 하지 않는다.
			if(ae instanceof ArithmeticException)
				System.out.println("true");
			System.out.println("ArithmeticException");
		}catch(Exception e) {	//ArithmeticException을 제외한 모든 예외가 처리된다.
			System.out.println("Exception");
		}
		System.out.println(6);
	}
}

출력결과

1
2
3
true
ArithmeticException
6

0개의 댓글

관련 채용 정보