Class Try

java.lang.Object
net.ME1312.Galaxi.Library.Try

public final class Try extends Object
Exception Handler Class
Remember that use of this class should always make your code shorter — it is not a replacement for native try-catch blocks
  • Field Details

  • Constructor Details

    • Try

      public Try(Consumer<Throwable> suppressor)
      Handle specific exceptions
      Parameters:
      suppressor - Exception suppressor
  • Method Details

    • expect

      @SafeVarargs public static Try expect(Class<? extends Throwable>... exceptions)
      Handle specific exceptions
      Parameters:
      exceptions - Exception types to handle
      Returns:
      Exception Handler
    • run

      public boolean run(Try.Runnable code)
      Run some code
      Parameters:
      code - Code to run
      Returns:
      true if the code was run successfully (without exceptions)
    • run

      public boolean run(Try.Runnable code, Consumer<Throwable> err)
      Run some code
      Parameters:
      code - Code to run
      err - Code to run for handling exceptions
      Returns:
      true if the code was run successfully (without exceptions)
    • get

      public <T> T get(Try.Supplier<T> value)
      Get a value despite exceptions
      Parameters:
      value - Code to run
      Returns:
      The return value of that code (or null in the event of an exception)
    • get

      public <T> T get(Try.Supplier<T> value, T def)
      Get a value despite exceptions
      Parameters:
      value - Code to run
      def - Default value
      Returns:
      The return value of that code (or def in the event of an exception)
    • get

      public <T> T get(Try.Supplier<T> value, Consumer<Throwable> err, T def)
      Get a value despite exceptions
      Parameters:
      value - Code to run
      err - Code to run for handling exceptions
      def - Default value
      Returns:
      The return value of that code (or def in the event of an exception)
    • getOrConsume

      public <T> T getOrConsume(Try.Supplier<T> value, Consumer<Throwable> err)
      Get a value despite exceptions
      Parameters:
      value - Code to run
      err - Code to run for handling exceptions
      Returns:
      The return value of that code (or null in the event of an exception)
    • getOrSupply

      public <T> T getOrSupply(Try.Supplier<T> value, Supplier<? extends T> def)
      Get a value despite exceptions
      Parameters:
      value - Code to run
      def - Code to run for generating the default value in the event of an exception
      Returns:
      The return value of either code block
    • getOrFunction

      public <T> T getOrFunction(Try.Supplier<T> value, Function<Throwable,? extends T> def)
      Get a value despite exceptions
      Parameters:
      value - Code to run
      def - Code to run for handling exceptions and/or generating the default value in the event of an exception
      Returns:
      The return value of either code block
    • getOrFunction

      public <T> T getOrFunction(Try.Supplier<T> value, Consumer<Throwable> err, Supplier<? extends T> def)
      Get a value despite exceptions
      Parameters:
      value - Code to run
      err - Code to run for handling exceptions
      def - Code to run for generating the default value in the event of an exception
      Returns:
      The return value of either code block