Skip to content

Commit 52ac9f6

Browse files
author
Bruce Eckel
committed
Checked exceptions etc. for CFs and Streams
1 parent a2a7b53 commit 52ac9f6

4 files changed

Lines changed: 94 additions & 1 deletion

File tree

concurrent/Breakable.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ public String toString() {
1717
" [" + failcount + "]";
1818
}
1919
public static Breakable work(Breakable b) {
20-
if(--b.failcount == 0)
20+
if(--b.failcount == 0) {
21+
System.out.println(
22+
"Throwing Exception for " + b.id + "");
2123
throw new RuntimeException(
2224
"Breakable_" + b.id + " failed");
25+
}
2326
System.out.println(b);
2427
return b;
2528
}

concurrent/CompletableExceptions.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,28 @@ public static void main(String[] args) {
4848
}
4949
}
5050
/* Output:
51+
Throwing Exception for A
5152
Breakable_B [1]
53+
Throwing Exception for B
5254
Breakable_C [2]
5355
Breakable_C [1]
56+
Throwing Exception for C
5457
Breakable_D [3]
5558
Breakable_D [2]
5659
Breakable_D [1]
60+
Throwing Exception for D
5761
Breakable_E [4]
5862
Breakable_E [3]
5963
Breakable_E [2]
6064
Breakable_E [1]
6165
Breakable_F [1]
66+
Throwing Exception for F
6267
java.lang.RuntimeException: Breakable_F failed
6368
Breakable_G [1]
69+
Throwing Exception for G
6470
true
6571
Breakable_H [1]
72+
Throwing Exception for H
6673
true
6774
done? false
6875
java.lang.RuntimeException: forced

concurrent/StreamExceptions.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// concurrent/StreamExceptions.java
2+
// (c)2017 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
import java.util.concurrent.*;
6+
import java.util.stream.*;
7+
import onjava.Nap;
8+
9+
public class StreamExceptions {
10+
static Stream<Breakable>
11+
test(String id, int failcount) {
12+
return
13+
Stream.of(new Breakable(id, failcount))
14+
.map(Breakable::work)
15+
.map(Breakable::work)
16+
.map(Breakable::work)
17+
.map(Breakable::work);
18+
}
19+
public static void main(String[] args) {
20+
// No operations are even applied ...
21+
test("A", 1);
22+
test("B", 2);
23+
Stream<Breakable> c = test("C", 3);
24+
test("D", 4);
25+
test("E", 5);
26+
// ... until there's a terminal operation:
27+
System.out.println("Entering try");
28+
try {
29+
c.forEach(System.out::println); // [1]
30+
} catch(Exception e) {
31+
System.out.println(e.getMessage());
32+
}
33+
}
34+
}
35+
/* Output:
36+
Entering try
37+
Breakable_C [2]
38+
Breakable_C [1]
39+
Throwing Exception for C
40+
Breakable_C failed
41+
*/

concurrent/ThrowsChecked.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// concurrent/ThrowsChecked.java
2+
// (c)2017 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
import java.util.stream.*;
6+
import java.util.concurrent.*;
7+
8+
public class ThrowsChecked {
9+
class Checked extends Exception {}
10+
static ThrowsChecked nochecked(ThrowsChecked tc) {
11+
return tc;
12+
}
13+
static ThrowsChecked
14+
withchecked(ThrowsChecked tc) throws Checked {
15+
return tc;
16+
}
17+
static void testStream() {
18+
Stream.of(new ThrowsChecked())
19+
.map(ThrowsChecked::nochecked)
20+
// .map(ThrowsChecked::withchecked); // [1]
21+
.map(tc -> {
22+
try {
23+
return withchecked(tc);
24+
} catch(Checked e) {
25+
throw new RuntimeException(e);
26+
}
27+
});
28+
}
29+
static void testCompletableFuture() {
30+
CompletableFuture
31+
.completedFuture(new ThrowsChecked())
32+
.thenApply(ThrowsChecked::nochecked)
33+
// .thenApply(ThrowsChecked::withchecked); // [2]
34+
.thenApply(tc -> {
35+
try {
36+
return withchecked(tc);
37+
} catch(Checked e) {
38+
throw new RuntimeException(e);
39+
}
40+
});
41+
}
42+
}

0 commit comments

Comments
 (0)