Module kivakit.core

Interface UncheckedCode<Value>

All Superinterfaces:
Broadcaster, Listener, Mixin, NamedObject, Receiver, Repeater, RepeaterMixin, Transceiver, Transmitter
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface UncheckedCode<Value> extends RepeaterMixin
Removes exception handling from code that can throw a checked (or unchecked) Exception.

Methods are provided that provide exception handling. For example the code below executes the doIt() method and logs a warning and returns the default value false if the code throws an exception. This design allows the code to handle the checked exception in a succinct and readable way.

 boolean doIt() throws NumberFormatException
 {
     [...]
 }

    [...]

 return UncheckedCode.of(this::doIt).or(false, "Unable to do it");
 
Author:
jonathanl (shibo)
  • Method Details

    • of

      static <T> UncheckedCode<T> of(UncheckedCode<T> code)
    • orDefault

      default Value orDefault(Source<Value> defaultValue)
      Returns:
      The value returned by the code, or a default value if an exception is thrown
    • orDefault

      default Value orDefault(Value defaultValue, String message, Object... arguments)
      Parameters:
      defaultValue - A default value to return if the code throws an exception
      message - A warning message to give to the listener if an exception is thrown
      arguments - Arguments to interpolate into the message
      Returns:
      The value returned by the code, or the given default value if an exception is thrown
    • orDefault

      default Value orDefault(Listener listener, Value defaultValue, String message, Object... arguments)
      Parameters:
      defaultValue - A default value to return if the code throws an exception
      listener - A listener to broadcast a warning message to if an exception is thrown
      message - A warning message to give to the listener if an exception is thrown
      arguments - Arguments to interpolate into the message
      Returns:
      The value returned by the code, or the given default value if an exception is thrown
    • orDefault

      default Value orDefault(Value defaultValue)
      Returns:
      The value returned by the code, or a default value if an exception is thrown
    • orNull

      default Value orNull()
    • run

      Value run() throws Exception
      Returns:
      The value returned by the checked code
      Throws:
      Exception - The exception that might be thrown by the code