From 52ac9f6dff71335996b73dcb17e35f9e345e0e94 Mon Sep 17 00:00:00 2001 From: Bruce Eckel Date: Fri, 6 Jan 2017 15:03:36 -0800 Subject: [PATCH] Checked exceptions etc. for CFs and Streams --- concurrent/Breakable.java | 5 +++- concurrent/CompletableExceptions.java | 7 +++++ concurrent/StreamExceptions.java | 41 ++++++++++++++++++++++++++ concurrent/ThrowsChecked.java | 42 +++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 concurrent/StreamExceptions.java create mode 100644 concurrent/ThrowsChecked.java diff --git a/concurrent/Breakable.java b/concurrent/Breakable.java index efab6d59..a3b632d7 100644 --- a/concurrent/Breakable.java +++ b/concurrent/Breakable.java @@ -17,9 +17,12 @@ public class Breakable { " [" + failcount + "]"; } 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( "Breakable_" + b.id + " failed"); + } System.out.println(b); return b; } diff --git a/concurrent/CompletableExceptions.java b/concurrent/CompletableExceptions.java index 407247c0..b035c5cf 100644 --- a/concurrent/CompletableExceptions.java +++ b/concurrent/CompletableExceptions.java @@ -48,21 +48,28 @@ public class CompletableExceptions { } } /* Output: +Throwing Exception for A Breakable_B [1] +Throwing Exception for B Breakable_C [2] Breakable_C [1] +Throwing Exception for C Breakable_D [3] Breakable_D [2] Breakable_D [1] +Throwing Exception for D Breakable_E [4] Breakable_E [3] Breakable_E [2] Breakable_E [1] Breakable_F [1] +Throwing Exception for F java.lang.RuntimeException: Breakable_F failed Breakable_G [1] +Throwing Exception for G true Breakable_H [1] +Throwing Exception for H true done? false java.lang.RuntimeException: forced diff --git a/concurrent/StreamExceptions.java b/concurrent/StreamExceptions.java new file mode 100644 index 00000000..372e53fc --- /dev/null +++ b/concurrent/StreamExceptions.java @@ -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 + 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 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 +*/ diff --git a/concurrent/ThrowsChecked.java b/concurrent/ThrowsChecked.java new file mode 100644 index 00000000..bbebadd1 --- /dev/null +++ b/concurrent/ThrowsChecked.java @@ -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); + } + }); + } +}