-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter36.java
More file actions
61 lines (52 loc) Β· 1.82 KB
/
Chapter36.java
File metadata and controls
61 lines (52 loc) Β· 1.82 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package chapter36;
public class ExceptionEx2 {
//throwν€μλ : κ°μ (μμ)λ‘ μμΈ λ°μ
//ν¨μλ΄μμ μμΈμ²λ¦¬ ->throw
/*
public static int divide(int num,int num1) {
if(num1==0) {
//ArithmeticException e = new ArithmeticException();
//throw e;
throw new ArithmeticException("num1λ³μκ° 0μ
λλ€");
}
int d = num/num1;
return d;//νΈμΆν κ³³μΌλ‘ dλ³μμ κ°μ λλ €μ€λ€
}
*/
public static void divide(int num, int num1) {
try {
int result = num/num1;
System.out.println("κ²°κ³Όκ° = "+result);
if(num1==0) {
//ArithmeticException e = new ArithmeticException();
//throw e;
throw new ArithmeticException("num1λ³μκ° 0μ
λλ€");
}
}
catch(ArithmeticException e){
System.out.println("0μΌλ‘ λλ μ μμ΅λλ€.");
System.out.println(e.toString());
}
}
public static void main(String[] args) {
/*
int num = 10;
int num1 =0;
try {
int result = divide(num,num1);//ν¨μλ΄μμ λλ €μ€ λ°νκ°μ λ°μμ κ°μ§κ³ μλ€
System.out.println("κ²°κ³Όκ° = "+result);
}catch(ArithmeticException e){
System.out.println("0μΌλ‘ λλ μ μμ΅λλ€.");
}finally {
System.out.println("μμΈκ° λ°μνμ§ μμλ 무쑰건 μνν΄μΌ νλ€");
}
System.out.println("---------------------------");
System.out.println("main() ν¨μ μν λ");
*/
int num = 10;
int num1 =0;
divide(num,num1);
System.out.println("---------------------------");
System.out.println("main() ν¨μ μν λ");
}
}