-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArithExceptionEx.java
More file actions
41 lines (31 loc) · 1009 Bytes
/
Copy pathArithExceptionEx.java
File metadata and controls
41 lines (31 loc) · 1009 Bytes
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
32
33
34
35
36
37
38
39
40
41
package com.javaex.exception;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ArithExceptionEx {
public static void main(String[] args) {
// 스캐터로부터 정수 입력
// 100을 정수로 나눈다
double result = 0;
int num;
Scanner scanner = new Scanner(System.in);
System.out.println("정수 입력하세요:");
// 오류 발생 가능 영역
/*num = scanner.nextInt();
result = 100 / num;*/
// 오류 발생 가능 영역
try {
num = scanner.nextInt();
result = 100 / num;
} catch (InputMismatchException e) {
System.out.println("정수로 해주세요");
} catch (ArithmeticException e) {
System.out.println("0으로는 나눌 수 없어요");
} catch (Exception e) { // Exception e는 최종 처리. 맨 마지막에 두면 좋음.
e.printStackTrace();
} finally {
System.out.println("Finally");
}
System.out.println(result);
scanner.close();
}
}