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 type attribute can be add,update,fix,remove. Add Streams.failableStream(T...). Add FailableSupplier.nul(). Add Suppliers.nul(). + Add ExceptionUtils.throwUnchecked(T) where T extends Throwable, and deprecate Object version. Bump commons-parent from 58 to 64. Bump org.easymock:easymock from 5.1.0 to 5.2.0 #1104. 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 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 The Throwable type. + * @param throwable the throwable to test and throw or return. + * @return the given throwable. + * @since 3.14.0 + */ + public static 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