public final class SneakyThrows extends Object
Function
, Consumer
, Runnable
,
Supplier
, Predicate
and
BiPredicate
.
Examples:
interface Query {
Item findById(String id) throws IOException;
}
Query query = ...
List<Item> items = Arrays.asList("1", "2", "3")
.stream()
.map(throwingFunction(query::findById))
.collect(Collectors.toList());
Modifier and Type | Class and Description |
---|---|
static interface |
SneakyThrows.Consumer<V>
Throwable version of
Consumer . |
static interface |
SneakyThrows.Consumer2<V1,V2>
Two argument version of
SneakyThrows.Consumer . |
static interface |
SneakyThrows.Consumer3<V1,V2,V3>
Three argument version of
SneakyThrows.Consumer . |
static interface |
SneakyThrows.Consumer4<V1,V2,V3,V4>
Four argument version of
SneakyThrows.Consumer . |
static interface |
SneakyThrows.Consumer5<V1,V2,V3,V4,V5>
Five argument version of
SneakyThrows.Consumer . |
static interface |
SneakyThrows.Consumer6<V1,V2,V3,V4,V5,V6>
Six argument version of
SneakyThrows.Consumer . |
static interface |
SneakyThrows.Consumer7<V1,V2,V3,V4,V5,V6,V7>
Seven argument version of
SneakyThrows.Consumer . |
static interface |
SneakyThrows.Consumer8<V1,V2,V3,V4,V5,V6,V7,V8>
Seven argument version of
SneakyThrows.Consumer . |
static interface |
SneakyThrows.Function<V,R>
Throwable version of
Function . |
static interface |
SneakyThrows.Function2<V1,V2,R>
Throwable version of
BiFunction . |
static interface |
SneakyThrows.Function3<V1,V2,V3,R>
Function with three arguments.
|
static interface |
SneakyThrows.Function4<V1,V2,V3,V4,R>
Function with four arguments.
|
static interface |
SneakyThrows.Function5<V1,V2,V3,V4,V5,R>
Function with five arguments.
|
static interface |
SneakyThrows.Function6<V1,V2,V3,V4,V5,V6,R>
Function with six arguments.
|
static interface |
SneakyThrows.Function7<V1,V2,V3,V4,V5,V6,V7,R>
Function with seven arguments.
|
static interface |
SneakyThrows.Function8<V1,V2,V3,V4,V5,V6,V7,V8,R>
Function with seven arguments.
|
static interface |
SneakyThrows.Predicate<V>
Throwable version of
SneakyThrows.Predicate . |
static interface |
SneakyThrows.Predicate2<V1,V2>
Throwable version of
SneakyThrows.Predicate . |
static interface |
SneakyThrows.Runnable
Throwable version of
Runnable . |
static interface |
SneakyThrows.Supplier<V>
Throwable version of
Supplier . |
Modifier and Type | Method and Description |
---|---|
static boolean |
isFatal(Throwable x)
True if the given exception is one of
InterruptedException , LinkageError ,
ThreadDeath , VirtualMachineError . |
static RuntimeException |
propagate(Throwable x)
Throws any throwable 'sneakily' - you don't need to catch it, nor declare that you throw it
onwards.
|
static <V> SneakyThrows.Consumer<V> |
throwingConsumer(SneakyThrows.Consumer<V> action)
Factory method for throwing consumer.
|
static <V1,V2> SneakyThrows.Consumer2<V1,V2> |
throwingConsumer(SneakyThrows.Consumer2<V1,V2> action)
Factory method for throwing consumer.
|
static <V1,V2,V3> SneakyThrows.Consumer3<V1,V2,V3> |
throwingConsumer(SneakyThrows.Consumer3<V1,V2,V3> action)
Factory method for throwing consumer.
|
static <V1,V2,V3,V4> |
throwingConsumer(SneakyThrows.Consumer4<V1,V2,V3,V4> action)
Factory method for throwing consumer.
|
static <V1,V2,V3,V4,V5> |
throwingConsumer(SneakyThrows.Consumer5<V1,V2,V3,V4,V5> action)
Factory method for throwing consumer.
|
static <V1,V2,V3,V4,V5,V6> |
throwingConsumer(SneakyThrows.Consumer6<V1,V2,V3,V4,V5,V6> action)
Factory method for throwing consumer.
|
static <V1,V2,V3,V4,V5,V6,V7> |
throwingConsumer(SneakyThrows.Consumer7<V1,V2,V3,V4,V5,V6,V7> action)
Factory method for throwing consumer.
|
static <V1,V2,V3,V4,V5,V6,V7,V8> |
throwingConsumer(SneakyThrows.Consumer8<V1,V2,V3,V4,V5,V6,V7,V8> action)
Factory method for throwing consumer.
|
static <V,R> SneakyThrows.Function<V,R> |
throwingFunction(SneakyThrows.Function<V,R> fn)
Factory method for
SneakyThrows.Function and Function . |
static <V1,V2,R> SneakyThrows.Function2<V1,V2,R> |
throwingFunction(SneakyThrows.Function2<V1,V2,R> fn)
Factory method for
SneakyThrows.Function2 and BiFunction . |
static <V1,V2,V3,R> |
throwingFunction(SneakyThrows.Function3<V1,V2,V3,R> fn)
Factory method for throwing function.
|
static <V1,V2,V3,V4,R> |
throwingFunction(SneakyThrows.Function4<V1,V2,V3,V4,R> fn)
Factory method for throwing function.
|
static <V1,V2,V3,V4,V5,R> |
throwingFunction(SneakyThrows.Function5<V1,V2,V3,V4,V5,R> fn)
Factory method for throwing function.
|
static <V1,V2,V3,V4,V5,V6,R> |
throwingFunction(SneakyThrows.Function6<V1,V2,V3,V4,V5,V6,R> fn)
Factory method for throwing function.
|
static <V1,V2,V3,V4,V5,V6,V7,R> |
throwingFunction(SneakyThrows.Function7<V1,V2,V3,V4,V5,V6,V7,R> fn)
Factory method for throwing function.
|
static <V1,V2,V3,V4,V5,V6,V7,V8,R> |
throwingFunction(SneakyThrows.Function8<V1,V2,V3,V4,V5,V6,V7,V8,R> fn)
Factory method for throwing function.
|
static <V> SneakyThrows.Predicate<V> |
throwingPredicate(SneakyThrows.Predicate<V> predicate)
Factory method for predicate.
|
static <V1,V2> SneakyThrows.Predicate2<V1,V2> |
throwingPredicate(SneakyThrows.Predicate2<V1,V2> predicate)
Factory method for predicate.
|
static SneakyThrows.Runnable |
throwingRunnable(SneakyThrows.Runnable action)
Factory method for
Runnable . |
static <V> SneakyThrows.Supplier<V> |
throwingSupplier(SneakyThrows.Supplier<V> fn)
Factory method for
SneakyThrows.Supplier . |
public static <V> SneakyThrows.Predicate<V> throwingPredicate(SneakyThrows.Predicate<V> predicate)
V
- Type 1.predicate
- Predicate.public static <V1,V2> SneakyThrows.Predicate2<V1,V2> throwingPredicate(SneakyThrows.Predicate2<V1,V2> predicate)
V1
- Type 1.V2
- Type 2.predicate
- Predicate.public static SneakyThrows.Runnable throwingRunnable(SneakyThrows.Runnable action)
Runnable
.action
- Runnable.public static <V> SneakyThrows.Supplier<V> throwingSupplier(SneakyThrows.Supplier<V> fn)
SneakyThrows.Supplier
.V
- Resulting value.fn
- Supplier.public static <V,R> SneakyThrows.Function<V,R> throwingFunction(SneakyThrows.Function<V,R> fn)
SneakyThrows.Function
and Function
.V
- Input value.R
- Result value.fn
- Function.public static <V1,V2,R> SneakyThrows.Function2<V1,V2,R> throwingFunction(SneakyThrows.Function2<V1,V2,R> fn)
SneakyThrows.Function2
and BiFunction
.V1
- Input value.V2
- Input value.R
- Result value.fn
- Function.public static <V1,V2,V3,R> SneakyThrows.Function3<V1,V2,V3,R> throwingFunction(SneakyThrows.Function3<V1,V2,V3,R> fn)
V1
- Type 1.V2
- Type 2.V3
- Type 3.R
- Return type.fn
- SneakyThrows function.public static <V1,V2,V3,V4,R> SneakyThrows.Function4<V1,V2,V3,V4,R> throwingFunction(SneakyThrows.Function4<V1,V2,V3,V4,R> fn)
V1
- Type 1.V2
- Type 2.V3
- Type 3.V4
- Type 4.R
- Return type.fn
- SneakyThrows function.public static <V1,V2,V3,V4,V5,R> SneakyThrows.Function5<V1,V2,V3,V4,V5,R> throwingFunction(SneakyThrows.Function5<V1,V2,V3,V4,V5,R> fn)
V1
- Type 1.V2
- Type 2.V3
- Type 3.V4
- Type 4.V5
- Type 5.R
- Return type.fn
- SneakyThrows function.public static <V1,V2,V3,V4,V5,V6,R> SneakyThrows.Function6<V1,V2,V3,V4,V5,V6,R> throwingFunction(SneakyThrows.Function6<V1,V2,V3,V4,V5,V6,R> fn)
V1
- Type 1.V2
- Type 2.V3
- Type 3.V4
- Type 4.V5
- Type 5.V6
- Type 6.R
- Return type.fn
- SneakyThrows function.public static <V1,V2,V3,V4,V5,V6,V7,R> SneakyThrows.Function7<V1,V2,V3,V4,V5,V6,V7,R> throwingFunction(SneakyThrows.Function7<V1,V2,V3,V4,V5,V6,V7,R> fn)
V1
- Type 1.V2
- Type 2.V3
- Type 3.V4
- Type 4.V5
- Type 5.V6
- Type 6.V7
- Type 7.R
- Return type.fn
- SneakyThrows function.public static <V1,V2,V3,V4,V5,V6,V7,V8,R> SneakyThrows.Function8<V1,V2,V3,V4,V5,V6,V7,V8,R> throwingFunction(SneakyThrows.Function8<V1,V2,V3,V4,V5,V6,V7,V8,R> fn)
V1
- Type 1.V2
- Type 2.V3
- Type 3.V4
- Type 4.V5
- Type 5.V6
- Type 6.V7
- Type 7.V8
- Type 8.R
- Return type.fn
- SneakyThrows function.public static <V> SneakyThrows.Consumer<V> throwingConsumer(SneakyThrows.Consumer<V> action)
V
- Type 1.action
- SneakyThrows consumer.public static <V1,V2> SneakyThrows.Consumer2<V1,V2> throwingConsumer(SneakyThrows.Consumer2<V1,V2> action)
V1
- Type 1.V2
- Type 2.action
- SneakyThrows consumer.public static <V1,V2,V3> SneakyThrows.Consumer3<V1,V2,V3> throwingConsumer(SneakyThrows.Consumer3<V1,V2,V3> action)
V1
- Type 1.V2
- Type 2.V3
- Type 3.action
- SneakyThrows consumer.public static <V1,V2,V3,V4> SneakyThrows.Consumer4<V1,V2,V3,V4> throwingConsumer(SneakyThrows.Consumer4<V1,V2,V3,V4> action)
V1
- Type 1.V2
- Type 2.V3
- Type 3.V4
- Type 4.action
- SneakyThrows consumer.public static <V1,V2,V3,V4,V5> SneakyThrows.Consumer5<V1,V2,V3,V4,V5> throwingConsumer(SneakyThrows.Consumer5<V1,V2,V3,V4,V5> action)
V1
- Type 1.V2
- Type 2.V3
- Type 3.V4
- Type 4.V5
- Type 5.action
- SneakyThrows consumer.public static <V1,V2,V3,V4,V5,V6> SneakyThrows.Consumer6<V1,V2,V3,V4,V5,V6> throwingConsumer(SneakyThrows.Consumer6<V1,V2,V3,V4,V5,V6> action)
V1
- Type 1.V2
- Type 2.V3
- Type 3.V4
- Type 4.V5
- Type 5.V6
- Type 6.action
- SneakyThrows consumer.public static <V1,V2,V3,V4,V5,V6,V7> SneakyThrows.Consumer7<V1,V2,V3,V4,V5,V6,V7> throwingConsumer(SneakyThrows.Consumer7<V1,V2,V3,V4,V5,V6,V7> action)
V1
- Type 1.V2
- Type 2.V3
- Type 3.V4
- Type 4.V5
- Type 5.V6
- Type 6.V7
- Type 7.action
- SneakyThrows consumer.public static <V1,V2,V3,V4,V5,V6,V7,V8> SneakyThrows.Consumer8<V1,V2,V3,V4,V5,V6,V7,V8> throwingConsumer(SneakyThrows.Consumer8<V1,V2,V3,V4,V5,V6,V7,V8> action)
V1
- Type 1.V2
- Type 2.V3
- Type 3.V4
- Type 4.V5
- Type 5.V6
- Type 6.V7
- Type 7.V8
- Type 8.action
- SneakyThrows consumer.public static RuntimeException propagate(Throwable x)
Example usage:
public void run() { throw sneakyThrow(new IOException("You don't need to catch me!")); }
NB: The exception is not wrapped, ignored, swallowed, or redefined. The JVM actually does not know or care about the concept of a 'checked exception'. All this method does is hide the act of throwing a checked exception from the java compiler.
Note that this method has a return type of RuntimeException
; it is advised you always
call this
method as argument to the throw
statement to avoid compiler errors regarding no return
statement and similar problems. This method won't of course return an actual
RuntimeException
-
it never returns, it always throws the provided exception.
x
- The throwable to throw without requiring you to catch its type.public static boolean isFatal(Throwable x)
InterruptedException
, LinkageError
,
ThreadDeath
, VirtualMachineError
.x
- Exception to test.InterruptedException
, LinkageError
,
ThreadDeath
, VirtualMachineError
.Copyright © 2020. All rights reserved.