This commit is contained in:
Gary Gregory 2024-01-06 11:02:42 -05:00
parent adc70c15ce
commit f0c63993bb
1 changed files with 20 additions and 14 deletions

View File

@ -814,20 +814,22 @@ public class ExceptionUtils {
} }
/** /**
* Use to throw a checked exception without adding the exception to the throws * Throws the given (usually checked) exception without adding the exception to the throws
* clause of the calling method. This method prevents throws clause * clause of the calling method. This method prevents throws clause
* pollution and reduces the clutter of "Caused by" exceptions in the * inflation and reduces the clutter of "Caused by" exceptions in the
* stack trace. * stack trace.
* <p> * <p>
* The use of this technique may be controversial, but exceedingly useful to * The use of this technique may be controversial, but useful.
* library developers.
* </p> * </p>
* <pre> * <pre>
* public int propagateExample { // note that there is no throws clause * // There is no throws clause in the method signature.
* public int propagateExample() {
* try { * try {
* return invocation(); // throws IOException * // throws SomeCheckedException.
* } catch (Exception e) { * return invocation();
* return ExceptionUtils.rethrow(e); // propagates a checked exception * } catch (SomeCheckedException e) {
* // Propagates a checked exception and compiles to return an int.
* return ExceptionUtils.rethrow(e);
* } * }
* } * }
* </pre> * </pre>
@ -836,15 +838,19 @@ public class ExceptionUtils {
* checked exception in a RuntimeException: * checked exception in a RuntimeException:
* </p> * </p>
* <pre> * <pre>
* public int wrapExample { // note that there is no throws clause * // There is no throws clause in the method signature.
* public int wrapExample() {
* try { * try {
* return invocation(); // throws IOException * // throws IOException.
* return invocation();
* } catch (Error e) { * } catch (Error e) {
* throw e; * throw e;
* } catch (RuntimeException e) { * } catch (RuntimeException e) {
* throw e; // wraps a checked exception * // Throws an unchecked exception.
* throw e;
* } catch (Exception e) { * } catch (Exception e) {
* throw new UndeclaredThrowableException(e); // wraps a checked exception * // wraps a checked exception.
* throw new UndeclaredThrowableException(e);
* } * }
* } * }
* </pre> * </pre>
@ -863,8 +869,8 @@ public class ExceptionUtils {
* *
* @param throwable * @param throwable
* The throwable to rethrow. * The throwable to rethrow.
* @param <T> The type of the returned value. * @param <T> The type of the return value.
* @return Never actually returned, this generic type matches any type * @return Never actually returns, this generic type matches any type
* which the calling site requires. "Returning" the results of this * which the calling site requires. "Returning" the results of this
* method, as done in the propagateExample above, will satisfy the * method, as done in the propagateExample above, will satisfy the
* Java compiler requirement that all code paths return a value. * Java compiler requirement that all code paths return a value.