Skip to content

Commit cc68549

Browse files
committed
BAEL-3840 Suppressed Exceptions in Java
1 parent b0ba0e1 commit cc68549

3 files changed

Lines changed: 94 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}

0 commit comments

Comments
 (0)