-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathJavaException.java
46 lines (38 loc) · 1.15 KB
/
JavaException.java
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
import java.io.IOException;
public class JavaException {
public static void main(String[] args) {
int x = 10, y = 20;
int dx, dy;
try{
dx = x % 5;
dy = y/dx;
}catch(ArithmeticException ae){
System.out.println("Caught AE");
dx = 2;
dy = y/dx;
}
x = x/dx;
y = y/dy;
System.out.println(dx+" "+dy);
System.out.println(x+" "+y);
// 1.
// No matter what happens, the exception raised by
// finally will get through
try (MyResource myResource = new MyResource()) {
myResource.open();
throw new NullPointerException("try");
} catch (Exception e) {
throw new IllegalArgumentException("catch");
} finally {
throw new ClassCastException("finally");
}
}
}
class MyResource implements AutoCloseable {
public void open() throws IOException {
throw new IOException("open");
}
public void close() {
throw new ArithmeticException("close");
}
}