This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new 3a0d3b93a LANG-1647 (#1069)
3a0d3b93a is described below
commit 3a0d3b93a81fd8c795be4da3bc34f693f7ee9ed3
Author: Dimitrios Efthymiou <[email protected]>
AuthorDate: Sun Jul 2 20:34:48 2023 +0100
LANG-1647 (#1069)
* --created method that checks if a given Throwable represents a checked
exception.
--created method that checks if a given Throwable represents an unchecked
exception.
* PR change - removed use of local variable for tests
* I made ConcurrentUtils to use the new method
* Use final
---------
Co-authored-by: Gary Gregory <[email protected]>
---
.../commons/lang3/concurrent/ConcurrentUtils.java | 5 ++--
.../commons/lang3/exception/ExceptionUtils.java | 24 +++++++++++++++
.../lang3/exception/ExceptionUtilsTest.java | 35 ++++++++++++++++++++++
3 files changed, 61 insertions(+), 3 deletions(-)
diff --git
a/src/main/java/org/apache/commons/lang3/concurrent/ConcurrentUtils.java
b/src/main/java/org/apache/commons/lang3/concurrent/ConcurrentUtils.java
index 50917d713..bafbad67d 100644
--- a/src/main/java/org/apache/commons/lang3/concurrent/ConcurrentUtils.java
+++ b/src/main/java/org/apache/commons/lang3/concurrent/ConcurrentUtils.java
@@ -22,6 +22,7 @@ import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.Validate;
+import org.apache.commons.lang3.exception.ExceptionUtils;
/**
* An utility class providing functionality related to the {@code
@@ -140,9 +141,7 @@ public class ConcurrentUtils {
* checked exception
*/
static Throwable checkedException(final Throwable ex) {
- Validate.isTrue(ex != null && !(ex instanceof RuntimeException)
- && !(ex instanceof Error), "Not a checked exception: " + ex);
-
+ Validate.isTrue(ExceptionUtils.isChecked(ex), "Not a checked
exception: " + ex);
return ex;
}
diff --git
a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
index 08f038af9..26ffed3bd 100644
--- a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
+++ b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
@@ -972,6 +972,30 @@ public class ExceptionUtils {
throw new UndeclaredThrowableException(throwable);
}
+ /**
+ * Checks if a throwable represents a checked exception
+ *
+ * @param throwable
+ * The throwable to check.
+ * @return True if the given Throwable is a checked exception.
+ * @since 3.13
+ */
+ public static boolean isChecked(final Throwable throwable) {
+ return throwable != null && !(throwable instanceof Error) &&
!(throwable instanceof RuntimeException);
+ }
+
+ /**
+ * Checks if a throwable represents an unchecked exception
+ *
+ * @param throwable
+ * The throwable to check.
+ * @return True if the given Throwable is an unchecked exception.
+ * @since 3.13
+ */
+ public static boolean isUnchecked(final Throwable throwable) {
+ return !isChecked(throwable);
+ }
+
/**
* Public constructor allows an instance of {@link ExceptionUtils} to be
created, although that is not
* normally necessary.
diff --git
a/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java
b/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java
index b12d66950..bcb2762ee 100644
--- a/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java
@@ -850,4 +850,39 @@ public class ExceptionUtilsTest extends AbstractLangTest {
final Throwable t = assertThrows(Throwable.class, () ->
ExceptionUtils.wrapAndThrow(new TestThrowable()));
assertTrue(ExceptionUtils.hasCause(t, TestThrowable.class));
}
+
+ @Test
+ public void testIsChecked_null() {
+ assertFalse(ExceptionUtils.isChecked(null));
+ }
+
+ @Test
+ public void testIsChecked_unchecked() {
+ assertFalse(ExceptionUtils.isChecked(new IllegalArgumentException()));
+ }
+
+ @Test
+ public void testIsChecked_checked() {
+ assertTrue(ExceptionUtils.isChecked(new IOException()));
+ }
+
+ @Test
+ public void testIsChecked_error() {
+ assertFalse(ExceptionUtils.isChecked(new StackOverflowError()));
+ }
+
+ @Test
+ public void testIsUnchecked_unchecked() {
+ assertTrue(ExceptionUtils.isUnchecked(new IllegalArgumentException()));
+ }
+
+ @Test
+ public void testIsUnchecked_checked() {
+ assertFalse(ExceptionUtils.isUnchecked(new IOException()));
+ }
+
+ @Test
+ public void testIsUnchecked_error() {
+ assertTrue(ExceptionUtils.isUnchecked(new StackOverflowError()));
+ }
}