diff --git a/pom.xml b/pom.xml
index e54caf3..9fa1460 100644
--- a/pom.xml
+++ b/pom.xml
@@ -10,7 +10,7 @@
4.0.0
jre-utils
- 0.2.4
+ 0.2.5
JreUtils
jar
diff --git a/src/main/java/info/unterrainer/commons/jreutils/Exceptions.java b/src/main/java/info/unterrainer/commons/jreutils/Exceptions.java
index 8e03047..de3b3a3 100644
--- a/src/main/java/info/unterrainer/commons/jreutils/Exceptions.java
+++ b/src/main/java/info/unterrainer/commons/jreutils/Exceptions.java
@@ -39,7 +39,7 @@ public static void swallow(final Runnable runnable, final Class>... exceptions
public static T swallowReturning(final Supplier supplier, final Class>... exceptions) {
try {
return supplier.get();
- } catch (Exception e) {
+ } catch (Throwable e) {
if (!containsException(e, exceptions))
throw e;
return null;
@@ -95,7 +95,7 @@ public static T retryReturning(final int times, final long backOffInMillis,
for (int i = 0; i < times; i++)
try {
result = supplier.get();
- } catch (Exception e) {
+ } catch (Throwable e) {
if (!containsException(e, exceptions) || i == times - 1)
throw e;
try {
@@ -107,10 +107,19 @@ public static T retryReturning(final int times, final long backOffInMillis,
return result;
}
- public static boolean containsException(final Exception e, final Class>... exceptions) {
+ public static boolean containsException(final Throwable e, final Class>... exceptions) {
for (Class> omit : exceptions)
- if (omit.isAssignableFrom(e.getClass()))
+ if (equalsOrCauseEquals(e, omit))
return true;
return false;
}
+
+ private static boolean equalsOrCauseEquals(final Throwable e, final Class> omit) {
+ if (omit.isAssignableFrom(e.getClass()))
+ return true;
+ Throwable cause = e.getCause();
+ if (cause != null && equalsOrCauseEquals(cause, omit))
+ return true;
+ return false;
+ }
}
diff --git a/src/test/java/info/unterrainer/commons/jreutils/ExceptionsTests.java b/src/test/java/info/unterrainer/commons/jreutils/ExceptionsTests.java
index 3d6d3df..3832858 100644
--- a/src/test/java/info/unterrainer/commons/jreutils/ExceptionsTests.java
+++ b/src/test/java/info/unterrainer/commons/jreutils/ExceptionsTests.java
@@ -1,11 +1,14 @@
package info.unterrainer.commons.jreutils;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
import org.junit.jupiter.api.Test;
public class ExceptionsTests {
@Test
- public void BeforeTest() {
+ public void codeBeforeTest() {
try {
// Some method you don't have control over throwing an exception.
throw new IllegalArgumentException();
@@ -15,9 +18,34 @@ public void BeforeTest() {
}
@Test
- public void AfterTest() {
+ public void throwsExceptionWithoutSwallow() {
+ assertThrows(IllegalArgumentException.class, () -> {
+ // Some method you don't have control over throwing an exception.
+ throw new IllegalArgumentException();
+ });
+ }
+
+ @Test
+ public void swallowsWhenExceptionMatches() {
Exceptions.swallow(() -> {
throw new IllegalArgumentException();
}, IllegalArgumentException.class, NumberFormatException.class);
+ assertTrue(true);
+ }
+
+ @Test
+ public void swallowsWhenCauseMatches() {
+ Exceptions.swallow(() -> {
+ throw new IllegalArgumentException("", new NumberFormatException());
+ }, NumberFormatException.class);
+ assertTrue(true);
+ }
+
+ @Test
+ public void swallowsWhenMultiLayeredCauseMatches() {
+ Exceptions.swallow(() -> {
+ throw new IllegalArgumentException("", new IllegalArgumentException("", new NumberFormatException()));
+ }, NumberFormatException.class);
+ assertTrue(true);
}
}