Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

chapter01 : exception 본문

JAVA/chapter19_exception

chapter01 : exception

GAWON 2023. 5. 30. 09:27
package org.joonzis.ex;

import java.util.Scanner;

/*
 * ArithmeticException
 * 정수를 0으로 나눌 경우 예외 발생
 */

public class Ex01_exception {
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int num1, num2;
		
		try {
			System.out.print("정수1 입력 >> ");
			num1 = sc.nextInt();
			System.out.print("정수2 입력 >> ");
			num2 = sc.nextInt();	// 0 입력 시
			System.out.println("덧셈 결과 >> " + (num1 + num2));
			System.out.println("뺄셈 결과 >> " + (num1 - num2));
			System.out.println("곱셈 결과 >> " + (num1 * num2));
			System.out.println("나눗셈 결과 >> " + (num1 / num2));	// 해당 위치에서 예외 발생
		} catch (ArithmeticException e) {	// Exception e 로 넣어도 된다.
			e.printStackTrace();	// 에러 문구 출력
			System.out.println("0으로 나눌 수 없습니다.");
		} finally {
			if(sc != null) {
				sc.close();
			}
		}
	}
}

'JAVA > chapter19_exception' 카테고리의 다른 글

chapter06 : exception  (0) 2023.05.30
chapter05 : exception  (0) 2023.05.30
chapter04 : exception  (0) 2023.05.30
chapter03 : exception  (0) 2023.05.30
chapter02 : exception  (0) 2023.05.30