You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/10/17 15:17:00 UTC
[commons-lang] 01/04: Add ExceptionUtils.throwUnchecked(T) where T extends Throwable, and deprecate Object version
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
commit 1abc85cb2db060b48c54537089b0a1e48524b5ad
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Oct 17 10:48:46 2023 -0400
Add ExceptionUtils.throwUnchecked(T) where T extends Throwable, and
deprecate Object version
---
src/changes/changes.xml | 1 +
.../commons/lang3/exception/ExceptionUtils.java | 20 ++++++++++++++++++++
2 files changed, 21 insertions(+)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 9ef8a801b..acfd3a581 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -74,6 +74,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Streams.failableStream(T...).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add FailableSupplier.nul().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Suppliers.nul().</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add ExceptionUtils.throwUnchecked(T) where T extends Throwable, and deprecate Object version.</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-parent from 58 to 64.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.easymock:easymock from 5.1.0 to 5.2.0 #1104.</action>
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 06440de01..0abb740d6 100644
--- a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
+++ b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
@@ -971,7 +971,9 @@ public class ExceptionUtils {
* @param throwable the throwable to test and throw or return.
* @return the given throwable.
* @since 3.13.0
+ * @deprecated Use {@link #throwUnchecked(Throwable)}.
*/
+ @Deprecated
public static <T> T throwUnchecked(final T throwable) {
if (throwable instanceof RuntimeException) {
throw (RuntimeException) throwable;
@@ -982,6 +984,24 @@ public class ExceptionUtils {
return throwable;
}
+ /**
+ * Tests whether the specified {@link Throwable} is unchecked and throws it if so.
+ *
+ * @param <T> The Throwable type.
+ * @param throwable the throwable to test and throw or return.
+ * @return the given throwable.
+ * @since 3.14.0
+ */
+ public static <T extends Throwable> T throwUnchecked(final T throwable) {
+ if (throwable instanceof RuntimeException) {
+ throw (RuntimeException) throwable;
+ }
+ if (throwable instanceof Error) {
+ throw (Error) throwable;
+ }
+ return throwable;
+ }
+
/**
* Throws a checked exception without adding the exception to the throws
* clause of the calling method. For checked exceptions, this method throws