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)
.close(ExpectationException::new);
}
of(Throwable),
reThrow(Class),
close(Function)| Modifier and Type | Method and Description |
|---|---|
T |
close()
Completes this revision and returns the associated exception unchanged.
|
<R> R |
close(Function<? super T,R> mapping)
Completes this revision, applies a given
mapping to the
associated exception, and returns its 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.
|
<R,X extends Throwable> |
reThrow(Class<X> xClass,
R result)
Rethrows the associated exception if it matches the given exception type,
otherwise a given result will be returned.
|
<X extends Throwable> |
throwIf(Predicate<? super T> condition,
Function<? super T,X> mapping)
Applies a given
mapping to the associated exception if the given
condition applies and throws the result, otherwise this Revision will be returned. |
<R,X extends Throwable> |
throwIf(Predicate<? super T> condition,
Function<? super T,X> mapping,
R result)
Applies a given
mapping to the associated exception if the given
condition applies and throws the result, otherwise a given result will be returned. |
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 if the given
condition applies and throws the result, 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 <R,X extends Throwable> R throwIf(Predicate<? super T> condition, Function<? super T,X> mapping, R result) throws X extends Throwable
mapping to the associated exception if the given
condition applies and throws the result, otherwise a given result will be returned.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.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.result - A predefined regular result in case the condition is not true.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)
.close(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),
close(Function)public final <R,X extends Throwable> R reThrow(Class<X> xClass, R result) throws X extends Throwable
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.xClass - The Class that represents the type of exception that is expected.result - A predefined regular result in case the exception is not of that type.X - the associated exception, cast to the expected type, if applicable.X extends Throwablepublic final <R> R close(Function<? super T,R> mapping)
mapping to the
associated exception, and returns its result.public final T close()
Copyright © 2023 Andreas Kluge-Kaindl, Bremen (de). All rights reserved.