File tree Expand file tree Collapse file tree
core-java-modules/core-java-exceptions-2/src
main/java/com/baeldung/suppressed
test/java/com/baeldung/suppressed Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .suppressed ;
2+
3+ public class ExceptionalResource implements AutoCloseable {
4+
5+ public void processSomething () {
6+ throw new NullPointerException ("Thrown from processSomething()" );
7+ }
8+
9+ @ Override
10+ public void close () throws Exception {
11+ throw new NullPointerException ("Thrown from close()" );
12+ }
13+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .suppressed ;
2+
3+ import java .io .FileInputStream ;
4+ import java .io .IOException ;
5+
6+ public class SuppressedExceptionsDemo {
7+
8+ public static void demoSuppressedException (String filePath ) throws IOException {
9+ FileInputStream fileIn = null ;
10+ try {
11+ fileIn = new FileInputStream (filePath );
12+ } catch (IOException e ) {
13+
14+ } finally {
15+ fileIn .close ();
16+ }
17+ }
18+
19+ public static void demoAddSuppressedException (String filePath ) throws IOException {
20+ Throwable firstException = null ;
21+ FileInputStream fileIn = null ;
22+ try {
23+ fileIn = new FileInputStream (filePath );
24+ } catch (IOException e ) {
25+ firstException = e ;
26+ } finally {
27+ try {
28+ fileIn .close ();
29+ } catch (NullPointerException npe ) {
30+ if (firstException != null ) {
31+ npe .addSuppressed (firstException );
32+ }
33+ }
34+ }
35+ }
36+
37+ public static void demoExceptionalResource () throws Exception {
38+ try (ExceptionalResource exceptionalResource = new ExceptionalResource ()) {
39+ exceptionalResource .processSomething ();
40+ }
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .suppressed ;
2+
3+ import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .assertThat ;
5+
6+ import java .io .FileNotFoundException ;
7+ import java .io .IOException ;
8+
9+ import org .junit .Test ;
10+ import static org .hamcrest .CoreMatchers .instanceOf ;
11+
12+ public class SuppressedExceptionsUnitTest {
13+
14+ @ Test (expected = NullPointerException .class )
15+ public void givenNonExistentFileName_whenAttemptFileOpen_thenNullPointerException () throws IOException {
16+ SuppressedExceptionsDemo .demoSuppressedException ("/non-existent-path/non-existent-file.txt" );
17+ }
18+
19+ @ Test
20+ public void givenNonExistentFileName_whenAttemptFileOpenStoreSuppressed_thenSuppressedExceptionAvailable () {
21+ try {
22+ SuppressedExceptionsDemo .demoAddSuppressedException ("/non-existent-path/non-existent-file.txt" );
23+ } catch (Exception e ) {
24+ assertEquals (1 , e .getSuppressed ().length );
25+ assertThat (e .getSuppressed ()[0 ], instanceOf (FileNotFoundException .class ));
26+ }
27+ }
28+
29+ @ Test
30+ public void whenUsingExceptionalResource_thenSuppressedExceptionAvailable () {
31+ try {
32+ SuppressedExceptionsDemo .demoExceptionalResource ();
33+ } catch (Exception e ) {
34+ assertEquals ("Thrown from processSomething()" , e .getMessage ());
35+ assertEquals (1 , e .getSuppressed ().length );
36+ assertEquals ("Thrown from close()" , e .getSuppressed ()[0 ].getMessage ());
37+ }
38+ }
39+ }
You can’t perform that action at this time.
0 commit comments