본문 바로가기
study/Java

[Java] 26. Exception5 (throw)

by 금이패런츠 2022. 3. 22.
728x90
반응형
package chap9;
/*
 * throw : 예외 발생 => 없는 예외를 강제발생
 * throws : 예외 처리 => 있는 예외를 처리
 * 
 * 예외 처리
 *   try catch finally : try 블럭에서 예외가 발생되면, catch 구문으로 제어 이동.
 * 				       finally 블럭은 무조건 실행 구문
 *   throws : 호출한 메서드로 예외 전달.
 *   
 * 예외 발생
 *   throw : 예외 강제 발생  
 * 
 */
public class ExceptionEx5 {
	public static void main(String[] args) {
		try {
			first();
		} catch (Exception e) {
			System.out.println("main : 숫자만 가능합니다.");
			e.printStackTrace();
		}
	}
	private static void first() throws Exception {
		System.out.println("first 메서드");
		second();
	}
	private static void second() throws Exception {
		System.out.println("second 메서드");
		try {
			System.out.println(Integer.parseInt("abc"));
		} catch (Exception e) {
			System.out.println("second : 숫자만 가능합니다.");
			e.printStackTrace();
			throw e; //예외 강제 발생
		}
	}
}
728x90
반응형