Skip to content

Commit 7d8d79a

Browse files
committed
simplified a few examples, removed redundant koan.
1 parent 624ba41 commit 7d8d79a

1 file changed

Lines changed: 28 additions & 52 deletions

File tree

‎koans/src/beginner/AboutExceptions.java‎

Lines changed: 28 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,103 +17,80 @@ public void catchCheckedExceptions() {
1717
String s;
1818
try {
1919
doStuff();
20-
s = "code run normally";
20+
s = "code ran normally";
2121
} catch(IOException e) {
2222
s = "exception thrown";
2323
}
24-
assertEquals(s,__);
24+
assertEquals(s, __);
2525
}
2626

2727
@Koan
2828
public void useFinally() {
2929
String s = "";
3030
try {
3131
doStuff();
32-
s = "code run normally";
32+
s += "code ran normally";
3333
} catch(IOException e) {
34-
s = "exception thrown";
34+
s += "exception thrown";
3535
} finally {
3636
s += " and finally ran as well";
3737
}
38-
assertEquals(s,__);
38+
assertEquals(s, __);
3939
}
4040

4141
@Koan
4242
public void finallyWithoutCatch() {
4343
String s = "";
4444
try {
45-
s = "code run normally";
45+
s = "code ran normally";
4646
} finally {
4747
s += " and finally ran as well";
4848
}
49-
assertEquals(s,__);
49+
assertEquals(s, __);
5050
}
5151

52-
private int k = 1, m = 2;
53-
54-
private void tryCatchFinallyWithVoidReturn() {
52+
private void tryCatchFinallyWithVoidReturn(StringBuilder whatHappened) {
5553
try {
54+
whatHappened.append("did something dangerous");
5655
doStuff();
5756
} catch(IOException e) {
58-
k += 1;
57+
whatHappened.append("; the catch block executed");
5958
return;
6059
} finally {
61-
// Will this code be executed despite the return statement above?
62-
k += 1;
60+
whatHappened.append(", but so did the finally!");
6361
}
6462
}
6563

6664
@Koan
6765
public void finallyIsAlwaysRan() {
68-
tryCatchFinallyWithVoidReturn();
69-
assertEquals(k,__);
70-
}
71-
72-
private int tryCatchFinallyReturningInt() {
73-
try {
74-
doStuff();
75-
} catch(IOException e) {
76-
k += 1;
77-
return k + m;
78-
} finally {
79-
m = 5;
80-
}
81-
return k;
66+
StringBuilder whatHappened = new StringBuilder();
67+
tryCatchFinallyWithVoidReturn(whatHappened);
68+
assertEquals(whatHappened.toString(), __);
8269
}
8370

84-
@Koan
85-
public void orderOfCatchFinallyAndReturn() {
86-
k = 1;
87-
assertEquals(tryCatchFinallyReturningInt(),__);
88-
// What is the order of executing catch, finally
89-
// and return statements in this case?
90-
assertEquals(k,__);
91-
assertEquals(m,__);
92-
}
93-
94-
private int i = 0, j = 1, l = 2;
95-
96-
private int returnStatementsEverywhere() {
71+
@SuppressWarnings("finally") // this is suppressed because returning in finally block is obviously a compiler warning
72+
private String returnStatementsEverywhere(StringBuilder whatHappened) {
9773
try {
74+
whatHappened.append("try");
9875
doStuff();
99-
return i++;
76+
return "from try";
10077
} catch (IOException e) {
101-
return j++;
78+
whatHappened.append(", catch");
79+
return "from catch";
10280
} finally {
103-
// Watch out! It is an EXTREMELY bad practice to
104-
// put return statement in finally block!
105-
return l++;
81+
whatHappened.append(", finally");
82+
// Think about how bad an idea it is to put a return statement in the finally block
83+
// DO NOT DO THIS!
84+
return "from finally";
10685
}
10786
}
10887

10988
@Koan
11089
public void returnInFinallyBlock() {
90+
StringBuilder whatHappened = new StringBuilder();
11191
// Which value will be returned here?
112-
assertEquals(returnStatementsEverywhere(),__);
113-
// Is only the returned variable modified?
114-
assertEquals(i,__);
115-
assertEquals(j,__);
116-
assertEquals(l,__);
92+
assertEquals(returnStatementsEverywhere(whatHappened), __);
93+
assertEquals(whatHappened.toString(), __);
11794
}
11895

11996
private void doUncheckedStuff() {
@@ -122,8 +99,7 @@ private void doUncheckedStuff() {
12299

123100
@Koan
124101
public void catchUncheckedExceptions() {
125-
// What do you need to do to catch the
126-
// unchecked exception?
102+
// What do you need to do to catch the unchecked exception?
127103
doUncheckedStuff();
128104
}
129105

0 commit comments

Comments
 (0)