public final class Revision<T extends Throwable> extends Object
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)
.finish(ExpectationException::new);
}
of(Throwable),
reThrow(Class),
finish(Function)| Modifier and Type | Method and Description |
|---|---|
T |
finish()
Returns the associated exception.
|
<R> R |
finish(Function<? super T,R> mapping)
Applies a given
mapping to the associated exception and returns the
result. |
static <T extends Throwable> |
of(T subject)
Returns a new instance to review and handle a given exception.
|
<X extends Throwable> |
reThrow(Class<X> xClass)
Rethrows the associated exception if it matches the given exception type.
|
<X extends Throwable> |
throwIf(Predicate<? super T> condition,
Function<? super T,X> mapping)
Applies a given
mapping to the associated exception and throws the
result if the given condition applies. |
public static <T extends Throwable> Revision<T> of(T subject)
T - the type of the given exceptionsubject - the exception to be handledpublic final <X extends Throwable> Revision<T> throwIf(Predicate<? super T> condition, Function<? super T,X> mapping) throws X extends Throwable
mapping to the associated exception and throws the
result if the given condition applies. Otherwise this Revision will be returned.X - The exception type that is intended as a result of the given mapping and that is thrown by this
method, if applicable.condition - A Predicate that is used to check the associated exception
for applicability.mapping - A Function that converts the associated exception to a
specific type of exception to be thrown at that point.Revision, which can be continued if no exception has been thrown.X - The converted exception, if present.X extends Throwablepublic final <X extends Throwable> Revision<T> reThrow(Class<X> xClass) throws X extends Throwable
Revision 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)
.finish(ExpectationException::new);
}
X - The type of exception that is expected and, if applicable, thrown by this method.xClass - The Class that represents the type of exception that is expected.Revision, which can be continued if no exception has been thrown.X - the associated exception, cast to the expected type, if applicable.X extends Throwableof(Throwable),
finish(Function)public final <R> R finish(Function<? super T,R> mapping)
mapping to the associated exception and returns the
result.public final T finish()
Copyright © 2021 Andreas Kluge-Kaindl, Bremen (de). All rights reserved.