Skip to content

Commit a55e718

Browse files
author
Gerald Unterrainer
committed
update tests of Exceptions.swallow and change containsException logic to recursively scan causes as well
1 parent 921f53c commit a55e718

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/main/java/info/unterrainer/commons/jreutils/Exceptions.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static void swallow(final Runnable runnable, final Class<?>... exceptions
3939
public static <T> T swallowReturning(final Supplier<T> supplier, final Class<?>... exceptions) {
4040
try {
4141
return supplier.get();
42-
} catch (Exception e) {
42+
} catch (Throwable e) {
4343
if (!containsException(e, exceptions))
4444
throw e;
4545
return null;
@@ -95,7 +95,7 @@ public static <T> T retryReturning(final int times, final long backOffInMillis,
9595
for (int i = 0; i < times; i++)
9696
try {
9797
result = supplier.get();
98-
} catch (Exception e) {
98+
} catch (Throwable e) {
9999
if (!containsException(e, exceptions) || i == times - 1)
100100
throw e;
101101
try {
@@ -107,10 +107,19 @@ public static <T> T retryReturning(final int times, final long backOffInMillis,
107107
return result;
108108
}
109109

110-
public static boolean containsException(final Exception e, final Class<?>... exceptions) {
110+
public static boolean containsException(final Throwable e, final Class<?>... exceptions) {
111111
for (Class<?> omit : exceptions)
112-
if (omit.isAssignableFrom(e.getClass()))
112+
if (equalsOrCauseEquals(e, omit))
113113
return true;
114114
return false;
115115
}
116+
117+
private static boolean equalsOrCauseEquals(final Throwable e, final Class<?> omit) {
118+
if (omit.isAssignableFrom(e.getClass()))
119+
return true;
120+
Throwable cause = e.getCause();
121+
if (cause != null && equalsOrCauseEquals(cause, omit))
122+
return true;
123+
return false;
124+
}
116125
}

src/test/java/info/unterrainer/commons/jreutils/ExceptionsTests.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,26 @@ public void throwsExceptionWithoutSwallow() {
2626
}
2727

2828
@Test
29-
public void swallowsException() {
29+
public void swallowsWhenExceptionMatches() {
3030
Exceptions.swallow(() -> {
3131
throw new IllegalArgumentException();
3232
}, IllegalArgumentException.class, NumberFormatException.class);
3333
assertTrue(true);
3434
}
35+
36+
@Test
37+
public void swallowsWhenCauseMatches() {
38+
Exceptions.swallow(() -> {
39+
throw new IllegalArgumentException("", new NumberFormatException());
40+
}, NumberFormatException.class);
41+
assertTrue(true);
42+
}
43+
44+
@Test
45+
public void swallowsWhenMultiLayeredCauseMatches() {
46+
Exceptions.swallow(() -> {
47+
throw new IllegalArgumentException("", new IllegalArgumentException("", new NumberFormatException()));
48+
}, NumberFormatException.class);
49+
assertTrue(true);
50+
}
3551
}

0 commit comments

Comments
 (0)