Skip to content

Commit

Permalink
Merge 511ef13 into 6b9964f
Browse files Browse the repository at this point in the history
  • Loading branch information
arturobernalg committed Mar 5, 2021
2 parents 6b9964f + 511ef13 commit dc11379
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/changes/changes.xml
Expand Up @@ -49,6 +49,7 @@ The <action> type attribute can be add,update,fix,remove.
<!-- FIX -->
<action issue="LANG-1645" type="fix" dev="aherbert" due-to="Alex Herbert">NumberUtils.createNumber to recognise hex integers prefixed with +.</action>
<action issue="LANG-1646" type="fix" dev="aherbert" due-to="Alex Herbert">NumberUtils.createNumber to return requested floating point type for zero.</action>
<action issue="LANG-1647" type="add" dev="abernal" due-to="Arturo Bernal">Check if an throwable is a checked exception.</action>
</release>

<release version="3.12.0" date="2021-02-26" description="New features and bug fixes (Java 8).">
Expand Down
Expand Up @@ -22,6 +22,7 @@
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.exception.ExceptionUtils;

/**
* <p>
Expand Down Expand Up @@ -142,8 +143,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
Expand Up @@ -586,6 +586,30 @@ public static int indexOfType(final Throwable throwable, final Class<? extends T
return indexOf(throwable, type, fromIndex, true);
}

/**
* Return whether the given throwable is a checked exception:
* that is, neither a RuntimeException nor an Error.
*
* <pre>
* ExceptionUtils.isChecked(new Exception()) = true
* ExceptionUtils.isChecked(new SQLException()) = true
* ExceptionUtils.isChecked(new RuntimeException()) = false
* ExceptionUtils.isChecked(new IllegalArgumentException("")) = false
* ExceptionUtils.isChecked(new Throwable()) = true
* ExceptionUtils.isChecked(null) = false
* </pre>
*
* @param throwable the throwable to check
* @return whether the throwable is a checked exception
* @see java.lang.Exception
* @see java.lang.RuntimeException
* @see java.lang.Error
* @since 3.12.1
*/
public static boolean isChecked(final Throwable throwable) {
return throwable != null && !(throwable instanceof RuntimeException || throwable instanceof Error);
}

//-----------------------------------------------------------------------
/**
* <p>Prints a compact stack trace for the root cause of a throwable
Expand Down
Expand Up @@ -31,6 +31,7 @@
import java.io.StringWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang3.test.NotVisibleExceptionFactory;
Expand Down Expand Up @@ -500,6 +501,17 @@ public void testIndexOfType_ThrowableClassInt() {
assertEquals(0, ExceptionUtils.indexOfType(withCause, Throwable.class, 0));
}

@Test
void isChecked() {
final Exception ex = null;
assertTrue(ExceptionUtils.isChecked(new Exception()));
assertTrue(ExceptionUtils.isChecked(new SQLException()));
assertFalse(ExceptionUtils.isChecked(new RuntimeException()));
assertFalse(ExceptionUtils.isChecked(new IllegalArgumentException("")));
assertTrue(ExceptionUtils.isChecked(new Throwable()));
assertFalse(ExceptionUtils.isChecked(ex));
}

//-----------------------------------------------------------------------
@Test
public void testPrintRootCauseStackTrace_Throwable() {
Expand Down

0 comments on commit dc11379

Please sign in to comment.