BAEL-3840 Adjust examples based on editor feedback
This commit is contained in:
parent
83539b1b3e
commit
d1ed8af579
@ -3,7 +3,7 @@ package com.baeldung.suppressed;
|
|||||||
public class ExceptionalResource implements AutoCloseable {
|
public class ExceptionalResource implements AutoCloseable {
|
||||||
|
|
||||||
public void processSomething() {
|
public void processSomething() {
|
||||||
throw new NullPointerException("Thrown from processSomething()");
|
throw new IllegalArgumentException("Thrown from processSomething()");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.baeldung.suppressed;
|
package com.baeldung.suppressed;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class SuppressedExceptionsDemo {
|
public class SuppressedExceptionsDemo {
|
||||||
@ -9,8 +10,8 @@ public class SuppressedExceptionsDemo {
|
|||||||
FileInputStream fileIn = null;
|
FileInputStream fileIn = null;
|
||||||
try {
|
try {
|
||||||
fileIn = new FileInputStream(filePath);
|
fileIn = new FileInputStream(filePath);
|
||||||
} catch (IOException e) {
|
} catch (FileNotFoundException e) {
|
||||||
throw new IOException(e.getMessage());
|
throw new IOException(e);
|
||||||
} finally {
|
} finally {
|
||||||
fileIn.close();
|
fileIn.close();
|
||||||
}
|
}
|
||||||
@ -30,6 +31,7 @@ public class SuppressedExceptionsDemo {
|
|||||||
if (firstException != null) {
|
if (firstException != null) {
|
||||||
npe.addSuppressed(firstException);
|
npe.addSuppressed(firstException);
|
||||||
}
|
}
|
||||||
|
throw npe;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ public class SuppressedExceptionsUnitTest {
|
|||||||
try {
|
try {
|
||||||
SuppressedExceptionsDemo.demoAddSuppressedException("/non-existent-path/non-existent-file.txt");
|
SuppressedExceptionsDemo.demoAddSuppressedException("/non-existent-path/non-existent-file.txt");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
assertThat(e, instanceOf(NullPointerException.class));
|
||||||
assertEquals(1, e.getSuppressed().length);
|
assertEquals(1, e.getSuppressed().length);
|
||||||
assertThat(e.getSuppressed()[0], instanceOf(FileNotFoundException.class));
|
assertThat(e.getSuppressed()[0], instanceOf(FileNotFoundException.class));
|
||||||
}
|
}
|
||||||
@ -31,8 +32,10 @@ public class SuppressedExceptionsUnitTest {
|
|||||||
try {
|
try {
|
||||||
SuppressedExceptionsDemo.demoExceptionalResource();
|
SuppressedExceptionsDemo.demoExceptionalResource();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
assertThat(e, instanceOf(IllegalArgumentException.class));
|
||||||
assertEquals("Thrown from processSomething()", e.getMessage());
|
assertEquals("Thrown from processSomething()", e.getMessage());
|
||||||
assertEquals(1, e.getSuppressed().length);
|
assertEquals(1, e.getSuppressed().length);
|
||||||
|
assertThat(e.getSuppressed()[0], instanceOf(NullPointerException.class));
|
||||||
assertEquals("Thrown from close()", e.getSuppressed()[0].getMessage());
|
assertEquals("Thrown from close()", e.getSuppressed()[0].getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user