Package science.aist.jack.exception
Class ExceptionUtils
- java.lang.Object
-
- science.aist.jack.exception.ExceptionUtils
-
public final class ExceptionUtils extends Object
Util class for Exception handling
- Since:
- 2.0
- Author:
- Andreas Pointner
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
failure(Throwable t)
Do a Static import of this function, and the use it to throw uncheck checked exception, by using e.g.:static <T,R>
Function<T,R>uncheck(ThrowingFunction<T,R> throwingFunction)
Allows the usage of a lambda expression that would throw an checked exception, and converts that into aFunction
that throws an uncheck functionstatic <T> Supplier<T>
uncheck(ThrowingSupplier<T> throwingSupplier)
Allows the usage of a lambda expression that would throw an checked exception, and converts that into aSupplier
static RuntimeException
unchecked(Throwable t)
Do a Static import of this function, and the use it to throw uncheck checked exception, by using e.g.:
-
-
-
Method Detail
-
unchecked
public static RuntimeException unchecked(Throwable t)
Do a Static import of this function, and the use it to throw uncheck checked exception, by using e.g.:
throw unchecked(new IOException());
Why would you ever want to wrap a checked exception with an unchecked one, when you can use this hack. e.g.
Instead of:
try { doSomething(); } catch (IOException ioe) { throw new RuntimeException(ioe); }
you can directly use:
try { doSomething(); } catch (IOException ioe) { throw ExceptionUtils.unchecked(ioe); // this will throw IOException directly }
- Parameters:
t
- the exception to be thrown- Returns:
- a runtimeException to avoid compiler checks
-
failure
public static <T> T failure(Throwable t)
Do a Static import of this function, and the use it to throw uncheck checked exception, by using e.g.:
return failure(new IOException());
Can also be used to fail a method with a check exception without the need to declare it to the method:
failure(new IOException());
- Type Parameters:
T
- any type, to satisfy the return type- Parameters:
t
- the exception to be thrown- Returns:
- nothing, as always an exception will be thrown
-
uncheck
public static <T,R> Function<T,R> uncheck(ThrowingFunction<T,R> throwingFunction)
Allows the usage of a lambda expression that would throw an checked exception, and converts that into a
Function
that throws an uncheck function.stream() .map(ExceptionUtils.uncheck(val -> val.iThrowAnException()));
- Type Parameters:
T
- input typeR
- output type- Parameters:
throwingFunction
- the function that should be executing declaring a checked exception- Returns:
- a function that converts the checked function into a unchecked one
-
uncheck
public static <T> Supplier<T> uncheck(ThrowingSupplier<T> throwingSupplier)
Allows the usage of a lambda expression that would throw an checked exception, and converts that into a
Supplier
Optional .orElseGet(ExceptionUtils.uncheck(() -> someMethodCall.thatThrowsException()));
- Type Parameters:
T
- the result type- Parameters:
throwingSupplier
- the supplier that should be executed declaring a checked exception- Returns:
- a supplier that converts the checked supplier into a unchecked one
-
-