Skip to content

Commit

Permalink
LANG-1647 (#1069)
Browse files Browse the repository at this point in the history
* --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]>
  • Loading branch information
orionlibs and garydgregory authored Jul 2, 2023
1 parent f909f46 commit 3a0d3b9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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
Expand Down Expand Up @@ -140,9 +141,7 @@ public static void handleCauseUnchecked(final ExecutionException ex) {
* 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,30 @@ public static <R> R wrapAndThrow(final Throwable throwable) {
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -850,4 +850,39 @@ public void testWrapAndUnwrapThrowable() {
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()));
}
}

0 comments on commit 3a0d3b9

Please sign in to comment.