Class Revision<T extends Throwable>
In general, there is little need for a special tool for differentiated handling of exceptions. The possibilities of Java’s own language elements (try-catch) are compact, expressive and efficient.
The situation is different if the cause of an exception is to be handled in a differentiated manner. This often results in extensive and sometimes difficult to read code. In any case, the regular code takes a back seat.
For example, in order not to have to deal with it all the time, we would like to temporarily wrap
checked exceptions into unchecked exceptions. However, we would then like to bring the
checked exceptions back to the fore in order to enforce a structured approach. A Revision
supports the latter, as the following code example shows:
try {
return somethingThatMayCauseAWrappedException();
} catch (final WrappedException caught) {
throw Revision.of(caught.getCause())
.reThrow(IOException.class)
.reThrow(SQLException.class)
.reThrow(URISyntaxException.class)
.close(ExpectationException::new);
}
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionfinal Tclose()Completes this revision and returns the associated exception unchanged.final <R> RCompletes this revision, applies a givenmappingto the associated exception, and returns its result.of(T subject) Returns a new instance to review and handle a given exception.Rethrows the associated exception if it matches the given exception type.final <R,X extends Throwable>
RRethrows the associated exception if it matches the given exception type, otherwise a given result will be returned.Applies a givenmappingto the associated exception if the givenconditionapplies and throws the result, otherwise this will be returned.final <R,X extends Throwable>
RApplies a givenmappingto the associated exception if the givenconditionapplies and throws the result, otherwise a given result will be returned.
-
Method Details
-
of
Returns a new instance to review and handle a given exception.- Type Parameters:
T- the type of the given exception- Parameters:
subject- the exception to be handled
-
throwIf
public final <X extends Throwable> Revision<T> throwIf(Predicate<? super T> condition, Function<? super T, X> mapping) throws XApplies a givenmappingto the associated exception if the givenconditionapplies and throws the result, otherwise this will be returned.- Type Parameters:
X- The exception type that is intended as a result of the given mapping and that is thrown by this method, if applicable.- Parameters:
condition- APredicatethat is used to check the associated exception for applicability.mapping- AFunctionthat converts the associated exception to a specific type of exception to be thrown at that point.- Returns:
- this, which can be continued if no exception has been thrown.
- Throws:
X- The converted exception, if present.
-
throwIf
public final <R,X extends Throwable> R throwIf(Predicate<? super T> condition, Function<? super T, X> mapping, R result) throws XApplies a givenmappingto the associated exception if the givenconditionapplies and throws the result, otherwise a given result will be returned.- Type Parameters:
R- The result type in case of a regular result.X- The exception type that is intended as a result of the given mapping and that is thrown by this method, if applicable.- Parameters:
condition- APredicatethat is used to check the associated exception for applicability.mapping- AFunctionthat converts the associated exception to a specific type of exception to be thrown at that point.result- A predefined regular result in case the condition is not true.- Returns:
- The predefined result.
- Throws:
X- The converted exception, if present.
-
reThrow
Rethrows the associated exception if it matches the given exception type. Otherwise, this will be returned. Example:try { return somethingThatMayCauseAWrappedException(); } catch (final WrappedException caught) { throw Revision.of(caught.getCause()) .reThrow(IOException.class) .reThrow(SQLException.class) .reThrow(URISyntaxException.class) .close(ExpectationException::new); }- Type Parameters:
X- The type of exception that is expected and, if applicable, thrown by this method.- Parameters:
xClass- TheClassthat represents the type of exception that is expected.- Returns:
- this, which can be continued if no exception has been thrown.
- Throws:
X- the associated exception, cast to the expected type, if applicable.- See Also:
-
reThrow
Rethrows the associated exception if it matches the given exception type, otherwise a given result will be returned.- Type Parameters:
R- The result type in case of a regular result.X- The type of exception that is expected and, if applicable, thrown by this method.- Parameters:
xClass- TheClassthat represents the type of exception that is expected.result- A predefined regular result in case the exception is not of that type.- Returns:
- The predefined result.
- Throws:
X- the associated exception, cast to the expected type, if applicable.
-
close
Completes this revision, applies a givenmappingto the associated exception, and returns its result. -
close
Completes this revision and returns the associated exception unchanged.
-