Package io.microsphere.lang.function
Interface ThrowableAction
-
- 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 ThrowableAction
A functional interface for actions that may throw aThrowable
.This interface is similar to
Runnable
, but allows the action to throw any exception. It can be used as a base for more specific interfaces that require exception handling.Example Usage
// Simple usage with a lambda expression ThrowableAction action = () -> { // some code that may throw an exception }; // Execute the action with default exception handling action.execute(); // Execute the action with custom exception handling action.execute(ex -> System.err.println("An error occurred: " + ex.getMessage()));
- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
Runnable
,Throwable
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description void
execute()
Executes the actionstatic void
execute(ThrowableAction action)
Executes the givenThrowableAction
with default exception handling.static void
execute(ThrowableAction action, java.util.function.Consumer<java.lang.Throwable> exceptionHandler)
Executes the givenThrowableAction
with a custom exception handler.default void
execute(java.util.function.Consumer<java.lang.Throwable> exceptionHandler)
Executesexecute()
with the customizedexception
handlingdefault void
handleException(java.lang.Throwable failure)
Handle any exception that theexecute()
method throws
-
-
-
Method Detail
-
execute
void execute() throws java.lang.Throwable
Executes the action- Throws:
java.lang.Throwable
- if met with error
-
execute
default void execute(java.util.function.Consumer<java.lang.Throwable> exceptionHandler)
Executesexecute()
with the customizedexception
handling- Parameters:
exceptionHandler
- the handler to handle anyexception
that theexecute()
method throws- Throws:
java.lang.NullPointerException
- ifexceptionHandler
isnull
-
handleException
default void handleException(java.lang.Throwable failure)
Handle any exception that theexecute()
method throws- Parameters:
failure
- the instance ofThrowable
-
execute
static void execute(ThrowableAction action)
Executes the givenThrowableAction
with default exception handling.If the action throws an exception during execution, it will be passed to the
handleException(Throwable)
method for handling.Example Usage
// Execute an action with default exception handling ThrowableAction action = () -> { if (someErrorCondition) { throw new IOException("Something went wrong"); } }; ThrowableAction.execute(action); // Uses default exception handling
- Parameters:
action
- theThrowableAction
to execute- Throws:
java.lang.NullPointerException
- if the provided action isnull
-
execute
static void execute(ThrowableAction action, java.util.function.Consumer<java.lang.Throwable> exceptionHandler) throws java.lang.NullPointerException
Executes the givenThrowableAction
with a custom exception handler.If the action throws an exception during execution, it will be passed to the provided
Consumer
exception handler for customized handling.Example Usage
// Execute an action with a custom exception handler ThrowableAction action = () -> { if (someErrorCondition) { throw new IOException("Something went wrong"); } }; ThrowableAction.execute(action, ex -> System.err.println("Error: " + ex.getMessage()));
- Parameters:
action
- theThrowableAction
to executeexceptionHandler
- the handler to manage any exceptions thrown by the action- Throws:
java.lang.NullPointerException
- if the provided action isnull
-
-