forked from matyb/java-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAboutExceptions.java
More file actions
166 lines (145 loc) · 4.62 KB
/
Copy pathAboutExceptions.java
File metadata and controls
166 lines (145 loc) · 4.62 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package beginner;
import com.sandwich.koan.Koan;
import java.io.IOException;
import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
public class AboutExceptions {
private void doStuff() throws IOException {
throw new IOException();
}
@Koan
public void catchCheckedExceptions() {
String s;
try {
doStuff();
s = "code ran normally";
} catch (IOException e) {
s = "exception thrown";
}
assertEquals(s, __);
}
@Koan
public void useFinally() {
String s = "";
try {
doStuff();
s += "code ran normally";
} catch (IOException e) {
s += "exception thrown";
} finally {
s += " and finally ran as well";
}
assertEquals(s, __);
}
@Koan
public void finallyWithoutCatch() {
String s = "";
try {
s = "code ran normally";
} finally {
s += " and finally ran as well";
}
assertEquals(s, __);
}
private void tryCatchFinallyWithVoidReturn(StringBuilder whatHappened) {
try {
whatHappened.append("did something dangerous");
doStuff();
} catch (IOException e) {
whatHappened.append("; the catch block executed");
return;
} finally {
whatHappened.append(", but so did the finally!");
}
}
@Koan
public void finallyIsAlwaysRan() {
StringBuilder whatHappened = new StringBuilder();
tryCatchFinallyWithVoidReturn(whatHappened);
assertEquals(whatHappened.toString(), __);
}
@SuppressWarnings("finally")
// this is suppressed because returning in finally block is obviously a compiler warning
private String returnStatementsEverywhere(StringBuilder whatHappened) {
try {
whatHappened.append("try");
doStuff();
return "from try";
} catch (IOException e) {
whatHappened.append(", catch");
return "from catch";
} finally {
whatHappened.append(", finally");
// Think about how bad an idea it is to put a return statement in the finally block
// DO NOT DO THIS!
return "from finally";
}
}
@Koan
public void returnInFinallyBlock() {
StringBuilder whatHappened = new StringBuilder();
// Which value will be returned here?
assertEquals(returnStatementsEverywhere(whatHappened), __);
assertEquals(whatHappened.toString(), __);
}
private void doUncheckedStuff() {
throw new RuntimeException();
}
@Koan
public void catchUncheckedExceptions() {
// What do you need to do to catch the unchecked exception?
doUncheckedStuff();
}
@SuppressWarnings("serial")
static class ParentException extends Exception {
}
@SuppressWarnings("serial")
static class ChildException extends ParentException {
}
private void throwIt() throws ParentException {
throw new ChildException();
}
@Koan
public void catchOrder() {
String s = "";
try {
throwIt();
} catch (ChildException e) {
s = "ChildException";
} catch (ParentException e) {
s = "ParentException";
}
assertEquals(s, __);
}
@Koan
public void failArgumentValidationWithAnIllegalArgumentException() {
// This koan demonstrates the use of exceptions in argument validation
String s = "";
try {
s += validateUsingIllegalArgumentException(null);
} catch (IllegalArgumentException ex) {
s = "caught an IllegalArgumentException";
}
assertEquals(s, __);
}
@Koan
public void passArgumentValidationWithAnIllegalArgumentException() {
// This koan demonstrates the use of exceptions in argument validation
String s = "";
try {
s += validateUsingIllegalArgumentException("valid");
} catch (IllegalArgumentException ex) {
s = "caught an IllegalArgumentException";
}
assertEquals(s, __);
}
private int validateUsingIllegalArgumentException(String str) {
// This is effective and both the evaluation and the error message
// can be tailored which can be particularly handy if you're guarding
// against more than null values
if (null == str) {
throw new IllegalArgumentException("str should not be null");
}
return str.length();
}
}