Checked exceptions etc. for CFs and Streams
This commit is contained in:
parent
a2a7b53b95
commit
52ac9f6dff
@ -17,9 +17,12 @@ public class Breakable {
|
|||||||
" [" + failcount + "]";
|
" [" + failcount + "]";
|
||||||
}
|
}
|
||||||
public static Breakable work(Breakable b) {
|
public static Breakable work(Breakable b) {
|
||||||
if(--b.failcount == 0)
|
if(--b.failcount == 0) {
|
||||||
|
System.out.println(
|
||||||
|
"Throwing Exception for " + b.id + "");
|
||||||
throw new RuntimeException(
|
throw new RuntimeException(
|
||||||
"Breakable_" + b.id + " failed");
|
"Breakable_" + b.id + " failed");
|
||||||
|
}
|
||||||
System.out.println(b);
|
System.out.println(b);
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
@ -48,21 +48,28 @@ public class CompletableExceptions {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Output:
|
/* Output:
|
||||||
|
Throwing Exception for A
|
||||||
Breakable_B [1]
|
Breakable_B [1]
|
||||||
|
Throwing Exception for B
|
||||||
Breakable_C [2]
|
Breakable_C [2]
|
||||||
Breakable_C [1]
|
Breakable_C [1]
|
||||||
|
Throwing Exception for C
|
||||||
Breakable_D [3]
|
Breakable_D [3]
|
||||||
Breakable_D [2]
|
Breakable_D [2]
|
||||||
Breakable_D [1]
|
Breakable_D [1]
|
||||||
|
Throwing Exception for D
|
||||||
Breakable_E [4]
|
Breakable_E [4]
|
||||||
Breakable_E [3]
|
Breakable_E [3]
|
||||||
Breakable_E [2]
|
Breakable_E [2]
|
||||||
Breakable_E [1]
|
Breakable_E [1]
|
||||||
Breakable_F [1]
|
Breakable_F [1]
|
||||||
|
Throwing Exception for F
|
||||||
java.lang.RuntimeException: Breakable_F failed
|
java.lang.RuntimeException: Breakable_F failed
|
||||||
Breakable_G [1]
|
Breakable_G [1]
|
||||||
|
Throwing Exception for G
|
||||||
true
|
true
|
||||||
Breakable_H [1]
|
Breakable_H [1]
|
||||||
|
Throwing Exception for H
|
||||||
true
|
true
|
||||||
done? false
|
done? false
|
||||||
java.lang.RuntimeException: forced
|
java.lang.RuntimeException: forced
|
||||||
|
41
concurrent/StreamExceptions.java
Normal file
41
concurrent/StreamExceptions.java
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
// concurrent/StreamExceptions.java
|
||||||
|
// (c)2017 MindView LLC: see Copyright.txt
|
||||||
|
// We make no guarantees that this code is fit for any purpose.
|
||||||
|
// Visit http://OnJava8.com for more book information.
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
import java.util.stream.*;
|
||||||
|
import onjava.Nap;
|
||||||
|
|
||||||
|
public class StreamExceptions {
|
||||||
|
static Stream<Breakable>
|
||||||
|
test(String id, int failcount) {
|
||||||
|
return
|
||||||
|
Stream.of(new Breakable(id, failcount))
|
||||||
|
.map(Breakable::work)
|
||||||
|
.map(Breakable::work)
|
||||||
|
.map(Breakable::work)
|
||||||
|
.map(Breakable::work);
|
||||||
|
}
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// No operations are even applied ...
|
||||||
|
test("A", 1);
|
||||||
|
test("B", 2);
|
||||||
|
Stream<Breakable> c = test("C", 3);
|
||||||
|
test("D", 4);
|
||||||
|
test("E", 5);
|
||||||
|
// ... until there's a terminal operation:
|
||||||
|
System.out.println("Entering try");
|
||||||
|
try {
|
||||||
|
c.forEach(System.out::println); // [1]
|
||||||
|
} catch(Exception e) {
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* Output:
|
||||||
|
Entering try
|
||||||
|
Breakable_C [2]
|
||||||
|
Breakable_C [1]
|
||||||
|
Throwing Exception for C
|
||||||
|
Breakable_C failed
|
||||||
|
*/
|
42
concurrent/ThrowsChecked.java
Normal file
42
concurrent/ThrowsChecked.java
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
// concurrent/ThrowsChecked.java
|
||||||
|
// (c)2017 MindView LLC: see Copyright.txt
|
||||||
|
// We make no guarantees that this code is fit for any purpose.
|
||||||
|
// Visit http://OnJava8.com for more book information.
|
||||||
|
import java.util.stream.*;
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
|
public class ThrowsChecked {
|
||||||
|
class Checked extends Exception {}
|
||||||
|
static ThrowsChecked nochecked(ThrowsChecked tc) {
|
||||||
|
return tc;
|
||||||
|
}
|
||||||
|
static ThrowsChecked
|
||||||
|
withchecked(ThrowsChecked tc) throws Checked {
|
||||||
|
return tc;
|
||||||
|
}
|
||||||
|
static void testStream() {
|
||||||
|
Stream.of(new ThrowsChecked())
|
||||||
|
.map(ThrowsChecked::nochecked)
|
||||||
|
// .map(ThrowsChecked::withchecked); // [1]
|
||||||
|
.map(tc -> {
|
||||||
|
try {
|
||||||
|
return withchecked(tc);
|
||||||
|
} catch(Checked e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
static void testCompletableFuture() {
|
||||||
|
CompletableFuture
|
||||||
|
.completedFuture(new ThrowsChecked())
|
||||||
|
.thenApply(ThrowsChecked::nochecked)
|
||||||
|
// .thenApply(ThrowsChecked::withchecked); // [2]
|
||||||
|
.thenApply(tc -> {
|
||||||
|
try {
|
||||||
|
return withchecked(tc);
|
||||||
|
} catch(Checked e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user