-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathExceptionExamples.java
More file actions
56 lines (44 loc) · 1.45 KB
/
ExceptionExamples.java
File metadata and controls
56 lines (44 loc) · 1.45 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
package ocp.chapter.ten;
class NoMoreCarrotsException extends Exception {
NoMoreCarrotsException() {
super("NO MORE CARROTS!");
}
}
interface Animal {
void good() throws NoMoreCarrotsException;
}
public class ExceptionExamples implements Animal {
private String string;
public static void main(String[] args) {
bad();
}
private static void bad() throws Error {
try {
nice();
} catch (NoMoreCarrotsException e) {
System.out.println(e.getMessage());
System.out.println("HA NoMoreCarrots...");
} catch (java.io.IOException e) {
System.out.println("IO? Not printed...");
}
try {
eatCarrot();
} catch (NullPointerException | IllegalArgumentException | Error e) { // Handling and declaring Error and its subclasses should not be done, but it works.
System.out.println(e);
System.out.println("HA NullPointer!");
}
throw new Error("OH NO");
}
public static void nice() throws NoMoreCarrotsException, java.io.IOException {
System.out.println("NICE");
throw new NoMoreCarrotsException();
}
public void good() {
System.out.println("GOOD");
}
private static void eatCarrot() {
ExceptionExamples exceptionExamples = new ExceptionExamples();
exceptionExamples.good();
exceptionExamples.string.length();
}
}