diff --git a/core-java/pom.xml b/core-java/pom.xml
index 463b65a4ce..661e787149 100644
--- a/core-java/pom.xml
+++ b/core-java/pom.xml
@@ -454,7 +454,7 @@
2.8.2
- 3.5
+ 3.9
2.5
3.6.1
1.0.3
diff --git a/core-java/src/main/java/com/baeldung/exceptions/RootCauseFinder.java b/core-java/src/main/java/com/baeldung/exceptions/RootCauseFinder.java
index e05dc7a6cd..064ae27ac1 100644
--- a/core-java/src/main/java/com/baeldung/exceptions/RootCauseFinder.java
+++ b/core-java/src/main/java/com/baeldung/exceptions/RootCauseFinder.java
@@ -10,10 +10,13 @@ import java.util.Objects;
*/
public class RootCauseFinder {
+ private RootCauseFinder() {
+ }
+
public static Throwable findCauseUsingPlainJava(Throwable throwable) {
Objects.requireNonNull(throwable);
Throwable rootCause = throwable;
- while (rootCause.getCause() != null) {
+ while (rootCause.getCause() != null && rootCause.getCause() != rootCause) {
rootCause = rootCause.getCause();
}
return rootCause;
diff --git a/core-java/src/test/java/com/baeldung/exceptions/RootCauseFinderTest.java b/core-java/src/test/java/com/baeldung/exceptions/RootCauseFinderTest.java
index cfac81b812..5f61a39ad1 100644
--- a/core-java/src/test/java/com/baeldung/exceptions/RootCauseFinderTest.java
+++ b/core-java/src/test/java/com/baeldung/exceptions/RootCauseFinderTest.java
@@ -75,6 +75,15 @@ public class RootCauseFinderTest {
}
}
+ @Test
+ public void givenNullDate_whenFindingRootCauseUsingApacheCommons_thenRootCauseNotFound() {
+ try {
+ AgeCalculator.calculateAge(null);
+ } catch (Exception ex) {
+ assertTrue(ExceptionUtils.getRootCause(ex) instanceof IllegalArgumentException);
+ }
+ }
+
@Test
public void givenWrongFormatDate_whenFindingRootCauseUsingGuava_thenRootCauseFound() {
try {
@@ -93,4 +102,13 @@ public class RootCauseFinderTest {
}
}
+ @Test
+ public void givenNullDate_whenFindingRootCauseUsingGuava_thenRootCauseFound() {
+ try {
+ AgeCalculator.calculateAge(null);
+ } catch (Exception ex) {
+ assertTrue(Throwables.getRootCause(ex) instanceof IllegalArgumentException);
+ }
+ }
+
}