From a681588bcc88d59b202faa6572fd09d2b646a86c Mon Sep 17 00:00:00 2001 From: amdegregorio Date: Mon, 17 Feb 2020 06:36:35 -0500 Subject: [PATCH 1/3] BAEL-3840 Suppressed Exceptions in Java --- .../suppressed/ExceptionalResource.java | 13 ++++++ .../suppressed/SuppressedExceptionsDemo.java | 42 +++++++++++++++++++ .../SuppressedExceptionsUnitTest.java | 39 +++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/ExceptionalResource.java create mode 100644 core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/SuppressedExceptionsDemo.java create mode 100644 core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/suppressed/SuppressedExceptionsUnitTest.java diff --git a/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/ExceptionalResource.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/ExceptionalResource.java new file mode 100644 index 0000000000..64b28eca8c --- /dev/null +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/ExceptionalResource.java @@ -0,0 +1,13 @@ +package com.baeldung.suppressed; + +public class ExceptionalResource implements AutoCloseable { + + public void processSomething() { + throw new NullPointerException("Thrown from processSomething()"); + } + + @Override + public void close() throws Exception { + throw new NullPointerException("Thrown from close()"); + } +} diff --git a/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/SuppressedExceptionsDemo.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/SuppressedExceptionsDemo.java new file mode 100644 index 0000000000..d7f8732f21 --- /dev/null +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/SuppressedExceptionsDemo.java @@ -0,0 +1,42 @@ +package com.baeldung.suppressed; + +import java.io.FileInputStream; +import java.io.IOException; + +public class SuppressedExceptionsDemo { + + public static void demoSuppressedException(String filePath) throws IOException { + FileInputStream fileIn = null; + try { + fileIn = new FileInputStream(filePath); + } catch (IOException e) { + + } finally { + fileIn.close(); + } + } + + public static void demoAddSuppressedException(String filePath) throws IOException { + Throwable firstException = null; + FileInputStream fileIn = null; + try { + fileIn = new FileInputStream(filePath); + } catch (IOException e) { + firstException = e; + } finally { + try { + fileIn.close(); + } catch (NullPointerException npe) { + if (firstException != null) { + npe.addSuppressed(firstException); + } + } + } + } + + public static void demoExceptionalResource() throws Exception { + try (ExceptionalResource exceptionalResource = new ExceptionalResource()) { + exceptionalResource.processSomething(); + } + } +} diff --git a/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/suppressed/SuppressedExceptionsUnitTest.java b/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/suppressed/SuppressedExceptionsUnitTest.java new file mode 100644 index 0000000000..810292d131 --- /dev/null +++ b/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/suppressed/SuppressedExceptionsUnitTest.java @@ -0,0 +1,39 @@ +package com.baeldung.suppressed; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import java.io.FileNotFoundException; +import java.io.IOException; + +import org.junit.Test; +import static org.hamcrest.CoreMatchers.instanceOf; + +public class SuppressedExceptionsUnitTest { + + @Test(expected = NullPointerException.class) + public void givenNonExistentFileName_whenAttemptFileOpen_thenNullPointerException() throws IOException { + SuppressedExceptionsDemo.demoSuppressedException("/non-existent-path/non-existent-file.txt"); + } + + @Test + public void givenNonExistentFileName_whenAttemptFileOpenStoreSuppressed_thenSuppressedExceptionAvailable() { + try { + SuppressedExceptionsDemo.demoAddSuppressedException("/non-existent-path/non-existent-file.txt"); + } catch (Exception e) { + assertEquals(1, e.getSuppressed().length); + assertThat(e.getSuppressed()[0], instanceOf(FileNotFoundException.class)); + } + } + + @Test + public void whenUsingExceptionalResource_thenSuppressedExceptionAvailable() { + try { + SuppressedExceptionsDemo.demoExceptionalResource(); + } catch (Exception e) { + assertEquals("Thrown from processSomething()", e.getMessage()); + assertEquals(1, e.getSuppressed().length); + assertEquals("Thrown from close()", e.getSuppressed()[0].getMessage()); + } + } +} From 83539b1b3e5bc3c0674f599248bd6898502fc9df Mon Sep 17 00:00:00 2001 From: amdegregorio Date: Tue, 18 Feb 2020 16:07:26 -0500 Subject: [PATCH 2/3] BAEL-3840 Fix example for section 2.1. --- .../java/com/baeldung/suppressed/SuppressedExceptionsDemo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/SuppressedExceptionsDemo.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/SuppressedExceptionsDemo.java index d7f8732f21..c1a838a3fd 100644 --- a/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/SuppressedExceptionsDemo.java +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/SuppressedExceptionsDemo.java @@ -10,7 +10,7 @@ public class SuppressedExceptionsDemo { try { fileIn = new FileInputStream(filePath); } catch (IOException e) { - + throw new IOException(e.getMessage()); } finally { fileIn.close(); } From d1ed8af579ca502d637290538908b18f9256a226 Mon Sep 17 00:00:00 2001 From: amdegregorio Date: Fri, 28 Feb 2020 06:54:27 -0500 Subject: [PATCH 3/3] BAEL-3840 Adjust examples based on editor feedback --- .../java/com/baeldung/suppressed/ExceptionalResource.java | 2 +- .../com/baeldung/suppressed/SuppressedExceptionsDemo.java | 6 ++++-- .../baeldung/suppressed/SuppressedExceptionsUnitTest.java | 3 +++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/ExceptionalResource.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/ExceptionalResource.java index 64b28eca8c..8892e097b1 100644 --- a/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/ExceptionalResource.java +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/ExceptionalResource.java @@ -3,7 +3,7 @@ package com.baeldung.suppressed; public class ExceptionalResource implements AutoCloseable { public void processSomething() { - throw new NullPointerException("Thrown from processSomething()"); + throw new IllegalArgumentException("Thrown from processSomething()"); } @Override diff --git a/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/SuppressedExceptionsDemo.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/SuppressedExceptionsDemo.java index c1a838a3fd..6ff23fd041 100644 --- a/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/SuppressedExceptionsDemo.java +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/SuppressedExceptionsDemo.java @@ -1,6 +1,7 @@ package com.baeldung.suppressed; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; public class SuppressedExceptionsDemo { @@ -9,8 +10,8 @@ public class SuppressedExceptionsDemo { FileInputStream fileIn = null; try { fileIn = new FileInputStream(filePath); - } catch (IOException e) { - throw new IOException(e.getMessage()); + } catch (FileNotFoundException e) { + throw new IOException(e); } finally { fileIn.close(); } @@ -30,6 +31,7 @@ public class SuppressedExceptionsDemo { if (firstException != null) { npe.addSuppressed(firstException); } + throw npe; } } } diff --git a/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/suppressed/SuppressedExceptionsUnitTest.java b/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/suppressed/SuppressedExceptionsUnitTest.java index 810292d131..7664ee6847 100644 --- a/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/suppressed/SuppressedExceptionsUnitTest.java +++ b/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/suppressed/SuppressedExceptionsUnitTest.java @@ -21,6 +21,7 @@ public class SuppressedExceptionsUnitTest { try { SuppressedExceptionsDemo.demoAddSuppressedException("/non-existent-path/non-existent-file.txt"); } catch (Exception e) { + assertThat(e, instanceOf(NullPointerException.class)); assertEquals(1, e.getSuppressed().length); assertThat(e.getSuppressed()[0], instanceOf(FileNotFoundException.class)); } @@ -31,8 +32,10 @@ public class SuppressedExceptionsUnitTest { try { SuppressedExceptionsDemo.demoExceptionalResource(); } catch (Exception e) { + assertThat(e, instanceOf(IllegalArgumentException.class)); assertEquals("Thrown from processSomething()", e.getMessage()); assertEquals(1, e.getSuppressed().length); + assertThat(e.getSuppressed()[0], instanceOf(NullPointerException.class)); assertEquals("Thrown from close()", e.getSuppressed()[0].getMessage()); } }