File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change @@ -48,21 +48,28 @@ public static void main(String[] args) {
4848 }
4949}
5050/* Output:
51+ Throwing Exception for A
5152Breakable_B [1]
53+ Throwing Exception for B
5254Breakable_C [2]
5355Breakable_C [1]
56+ Throwing Exception for C
5457Breakable_D [3]
5558Breakable_D [2]
5659Breakable_D [1]
60+ Throwing Exception for D
5761Breakable_E [4]
5862Breakable_E [3]
5963Breakable_E [2]
6064Breakable_E [1]
6165Breakable_F [1]
66+ Throwing Exception for F
6267java.lang.RuntimeException: Breakable_F failed
6368Breakable_G [1]
69+ Throwing Exception for G
6470true
6571Breakable_H [1]
72+ Throwing Exception for H
6673true
6774done? false
6875java.lang.RuntimeException: forced
Original file line number Diff line number Diff line change 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+ */
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments