Module id.xfunction

Class Unchecked

Object
Unchecked

public class Unchecked extends Object
This class allows you to execute functions which throw checked exceptions in a way if they were throwing unchecked RuntimeException.

For example given method which throws checked Exception:


 int m() throws Exception {
     return 0;
 }
 
Instead of writing:

 try { m(); } catch (Exception e) { throw new RuntimeException(e); }
 
You can use this class:

 getInt(this::m);
 
  • Constructor Details

    • Unchecked

      public Unchecked()
  • Method Details

    • get

      public static <R, E extends Exception> R get(ThrowingSupplier<R,E> s)
      Executes given supplier and catch all checked exceptions. If checked exception is thrown it is wrapped into unchecked RuntimeException and is thrown further.
    • getInt

      public static <E extends Exception> int getInt(ThrowingIntSupplier<E> s)
      Executes given int supplier and catch all checked exceptions. If checked exception is thrown it is wrapped into unchecked RuntimeException and is thrown further.
    • getBoolean

      public static <E extends Exception> boolean getBoolean(ThrowingBooleanSupplier<E> s)
      Executes given boolean supplier and catch all checked exceptions. If checked exception is thrown it is wrapped into unchecked RuntimeException and is thrown further.
    • run

      public static <E extends Exception> void run(ThrowingRunnable<E> s)
      Executes given runnable and catch all checked exceptions. If checked exception is thrown it is wrapped into unchecked RuntimeException and is thrown further.
    • apply

      public static <A, R, E extends Exception> R apply(ThrowingFunction<A,R,E> s, A a)
      Executes given function with a given argument and catch all checked exceptions. If checked exception is thrown it is wrapped into unchecked RuntimeException and is thrown further.
    • accept

      public static <T, E extends Exception> void accept(ThrowingConsumer<T,E> c, T t)
      Executes given consumer with a given argument and catch all checked exceptions. If checked exception is thrown it is wrapped into unchecked RuntimeException and is thrown further.
    • accept

      public static <T1, T2, E extends Exception> void accept(ThrowingBiConsumer<T1,T2,E> c, T1 t1, T2 t2)
      Executes given biconsumer with a given arguments and catch all checked exceptions. If checked exception is thrown it is wrapped into unchecked RuntimeException and is thrown further.
    • acceptInt

      public static <E extends Exception> void acceptInt(ThrowingIntConsumer<E> c, int t)
      Executes given consumer with a given argument and catch all checked exceptions. If checked exception is thrown it is wrapped into unchecked RuntimeException and is thrown further.
    • wrapRun

      public static <E extends Exception> Runnable wrapRun(ThrowingRunnable<E> r)
      Accepts ThrowingRunnable which throws checked exception and converts it to runnable which throws unchecked one
    • wrapGet

      public static <R, E extends Exception> Supplier<R> wrapGet(ThrowingSupplier<R,E> s)
      Accepts ThrowingSupplier which throws checked exception and converts it to Supplier which throws unchecked one
    • wrapGetInt

      public static <E extends Exception> IntSupplier wrapGetInt(ThrowingIntSupplier<E> s)
      Accepts ThrowingIntSupplier which throws checked exception and converts it to IntSupplier which throws unchecked one
    • wrapApply

      public static <A, R, E extends Exception> Function<A,R> wrapApply(ThrowingFunction<A,R,E> f)
      Accepts ThrowingFunction which throws checked exception and converts it to Function which throws unchecked one
    • wrapAccept

      public static <T, E extends Exception> Consumer<T> wrapAccept(ThrowingConsumer<T,E> c)
      Accepts ThrowingConsumer which throws checked exception and converts it to Consumer which throws unchecked one
    • wrapAccept

      public static <T, E extends Exception> Consumer<T> wrapAccept(ThrowingConsumer<T,E> c, Exception baseException)
      Accepts ThrowingConsumer which throws any exception and converts it to Consumer which will catch them and suppress into baseException
    • wrapAccept

      public static <T1, T2, E extends Exception> BiConsumer<T1,T2> wrapAccept(ThrowingBiConsumer<T1,T2,E> c)
      Accepts ThrowingConsumer which throws checked exception and converts it to Consumer which throws unchecked one
    • wrapAcceptInt

      public static <E extends Exception> IntConsumer wrapAcceptInt(ThrowingIntConsumer<E> c)
      Accepts ThrowingConsumer which throws checked exception and converts it to Consumer which throws unchecked one