본문 바로가기

Programming/[JAVA]

[JAVA] ArithmeticsException

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
package pack02;
 
import java.util.Scanner;
 
public class ArithmeticsException {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
 
        Scanner sc = new Scanner(System.in);
        int num1, num2;
        
        try {
            System.out.println("피제수를 입력하세요 : ");
            num1 = sc.nextInt();
            
            System.out.println("제수를 입력하세요 : "); //제수가 0일 경우 ArithmeticsException
            num2 = sc.nextInt();
            
            System.out.println(num1 + "을 " + num2 + "로 나눈 값이 " + num1/num2 + "이다.");            
        } catch (ArithmeticException e) {
            // TODO: handle exception
            System.out.println("피제수를 0으로 나눌 수 없다.("+ e +")");
            return;
        }
    }
}
 
cs