Skip to content

Commit 48b156b

Browse files
committed
BAEL-3840 Adjust examples based on editor feedback
1 parent d3bd2c4 commit 48b156b

3 files changed

Lines changed: 8 additions & 3 deletions

File tree

core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/ExceptionalResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public class ExceptionalResource implements AutoCloseable {
44

55
public void processSomething() {
6-
throw new NullPointerException("Thrown from processSomething()");
6+
throw new IllegalArgumentException("Thrown from processSomething()");
77
}
88

99
@Override

core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/suppressed/SuppressedExceptionsDemo.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.baeldung.suppressed;
22

33
import java.io.FileInputStream;
4+
import java.io.FileNotFoundException;
45
import java.io.IOException;
56

67
public class SuppressedExceptionsDemo {
@@ -9,8 +10,8 @@ public static void demoSuppressedException(String filePath) throws IOException {
910
FileInputStream fileIn = null;
1011
try {
1112
fileIn = new FileInputStream(filePath);
12-
} catch (IOException e) {
13-
throw new IOException(e.getMessage());
13+
} catch (FileNotFoundException e) {
14+
throw new IOException(e);
1415
} finally {
1516
fileIn.close();
1617
}
@@ -30,6 +31,7 @@ public static void demoAddSuppressedException(String filePath) throws IOExceptio
3031
if (firstException != null) {
3132
npe.addSuppressed(firstException);
3233
}
34+
throw npe;
3335
}
3436
}
3537
}

core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/suppressed/SuppressedExceptionsUnitTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public void givenNonExistentFileName_whenAttemptFileOpenStoreSuppressed_thenSupp
2121
try {
2222
SuppressedExceptionsDemo.demoAddSuppressedException("/non-existent-path/non-existent-file.txt");
2323
} catch (Exception e) {
24+
assertThat(e, instanceOf(NullPointerException.class));
2425
assertEquals(1, e.getSuppressed().length);
2526
assertThat(e.getSuppressed()[0], instanceOf(FileNotFoundException.class));
2627
}
@@ -31,8 +32,10 @@ public void whenUsingExceptionalResource_thenSuppressedExceptionAvailable() {
3132
try {
3233
SuppressedExceptionsDemo.demoExceptionalResource();
3334
} catch (Exception e) {
35+
assertThat(e, instanceOf(IllegalArgumentException.class));
3436
assertEquals("Thrown from processSomething()", e.getMessage());
3537
assertEquals(1, e.getSuppressed().length);
38+
assertThat(e.getSuppressed()[0], instanceOf(NullPointerException.class));
3639
assertEquals("Thrown from close()", e.getSuppressed()[0].getMessage());
3740
}
3841
}

0 commit comments

Comments
 (0)