Skip to content

Commit 74e0c0d

Browse files
author
Gerald Unterrainer
committed
Merge branch 'develop'
2 parents 60c23a7 + dd0dc25 commit 74e0c0d

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<modelVersion>4.0.0</modelVersion>
1212
<artifactId>jre-utils</artifactId>
13-
<version>0.2.4</version>
13+
<version>0.2.5</version>
1414
<name>JreUtils</name>
1515
<packaging>jar</packaging>
1616

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
}
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package info.unterrainer.commons.jreutils;
22

3+
import static org.junit.jupiter.api.Assertions.assertThrows;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
36
import org.junit.jupiter.api.Test;
47

58
public class ExceptionsTests {
69

710
@Test
8-
public void BeforeTest() {
11+
public void codeBeforeTest() {
912
try {
1013
// Some method you don't have control over throwing an exception.
1114
throw new IllegalArgumentException();
@@ -15,9 +18,34 @@ public void BeforeTest() {
1518
}
1619

1720
@Test
18-
public void AfterTest() {
21+
public void throwsExceptionWithoutSwallow() {
22+
assertThrows(IllegalArgumentException.class, () -> {
23+
// Some method you don't have control over throwing an exception.
24+
throw new IllegalArgumentException();
25+
});
26+
}
27+
28+
@Test
29+
public void swallowsWhenExceptionMatches() {
1930
Exceptions.swallow(() -> {
2031
throw new IllegalArgumentException();
2132
}, IllegalArgumentException.class, NumberFormatException.class);
33+
assertTrue(true);
34+
}
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);
2250
}
2351
}

0 commit comments

Comments
 (0)