Interface CheckedRunnable
-
- 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 CheckedRunnable
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description voidrun()This method performs the operation that may fail with a CheckedExceptionstatic Runnablesafe(CheckedRunnable runnableThatCanFailWithACheckedException)Wraps aCheckedRunnable(basically a lambda with no arguments that doesn't return any result and which throws a CheckedException) by returning a newRunnableinstance
The returnedRunnable.run()method delegates directly to therun()and catches any thrown checkedException's and rethrows them as aCheckedExceptionRethrownException
Unless you provide a context-message (usingsafe(String, CheckedRunnable)then any caught checkedException's message also becomes theCheckedExceptionRethrownException's message.
AnyRuntimeException's thrown aren't caught and the calling code will receive the originalRuntimeExceptionthrown.
Usage example:
Let's say we have a method calledsomeOperationthat cannot change, but which accepts aRunnablewith the purpose of the calling theRunnable.run().static Runnablesafe(String contextMessage, CheckedRunnable runnableThatCanFailWithACheckedException)Wraps aCheckedRunnable(basically a lambda with no arguments that doesn't return any result and which throws a CheckedException) by returning a newRunnableinstance
The returnedRunnable.run()method delegates directly to therun()and catches any thrown checkedException's and rethrows them as aCheckedExceptionRethrownException
AnyRuntimeException's thrown aren't caught and the calling code will receive the originalRuntimeExceptionthrown.
Usage example:
Let's say we have a method calledsomeOperationthat cannot change, but which accepts aRunnablewith the purpose of the calling theRunnable.run().
-
-
-
Method Detail
-
safe
static Runnable safe(CheckedRunnable runnableThatCanFailWithACheckedException)
Wraps aCheckedRunnable(basically a lambda with no arguments that doesn't return any result and which throws a CheckedException) by returning a newRunnableinstance
The returnedRunnable.run()method delegates directly to therun()and catches any thrown checkedException's and rethrows them as aCheckedExceptionRethrownException
Unless you provide a context-message (usingsafe(String, CheckedRunnable)then any caught checkedException's message also becomes theCheckedExceptionRethrownException's message.
AnyRuntimeException's thrown aren't caught and the calling code will receive the originalRuntimeExceptionthrown.
Usage example:
Let's say we have a method calledsomeOperationthat cannot change, but which accepts aRunnablewith the purpose of the calling theRunnable.run().
The problem willpublic void someOperation(Runnable operation) { // ... Logic ... operation.run(); // ... More logic --- }Runnable.run()occurs whenRunnablecalls any API that throws a checkedException, e.g. theFileAPI.
SinceRunnable.run()doesn't define that it throws anyException's we're forced to add a try/catch to handle theIOExceptionfor the code to compile:someOperation(() -> { try { // Logic that uses the File API } catch (IOException e) { throw new RuntimeException(e); } }));
This is where theCheckedRunnablecomes to the aid as itsrun()methods defines that it throws a CheckedExceptionand itssafe(CheckedRunnable)will return a newRunnableinstance with aRunnable.run()method that ensures that therun()method is called and any checkedException's thrown will be caught and rethrown as aCheckedExceptionRethrownException:someOperation(CheckedRunnable.safe(() -> { // Logic that uses the File API that throws IOException }));- Parameters:
runnableThatCanFailWithACheckedException- theCheckedRunnableinstance that will be wrapped as aRunnable- Returns:
- a
Runnablewith aRunnable.run()method that ensures that therun()method is called whenRunnable.run()and any checkedException's thrown will be caught and rethrown as aCheckedExceptionRethrownException.
AnyRuntimeException's thrown aren't caught and the calling code will receive the originalRuntimeExceptionthrown. - Throws:
CheckedExceptionRethrownException- in case therun()method throws a checkedException
-
safe
static Runnable safe(String contextMessage, CheckedRunnable runnableThatCanFailWithACheckedException)
Wraps aCheckedRunnable(basically a lambda with no arguments that doesn't return any result and which throws a CheckedException) by returning a newRunnableinstance
The returnedRunnable.run()method delegates directly to therun()and catches any thrown checkedException's and rethrows them as aCheckedExceptionRethrownException
AnyRuntimeException's thrown aren't caught and the calling code will receive the originalRuntimeExceptionthrown.
Usage example:
Let's say we have a method calledsomeOperationthat cannot change, but which accepts aRunnablewith the purpose of the calling theRunnable.run().
The problem willpublic void someOperation(Runnable operation) { // ... Logic ... operation.run(); // ... More logic --- }Runnable.run()occurs whenRunnablecalls any API that throws a checkedException, e.g. theFileAPI.
SinceRunnable.run()doesn't define that it throws anyException's we're forced to add a try/catch to handle theIOExceptionfor the code to compile:someOperation(() -> { try { // Logic that uses the File API } catch (IOException e) { throw new RuntimeException(e); } }));
This is where theCheckedRunnablecomes to the aid as itsrun()methods defines that it throws a CheckedExceptionand itssafe(CheckedRunnable)will return a newRunnableinstance with aRunnable.run()method that ensures that therun()method is called and any checkedException's thrown will be caught and rethrown as aCheckedExceptionRethrownException:someOperation(CheckedRunnable.safe(msg("Processing file {}", fileName), () -> { // Logic that uses the File API that throws IOException }));- Parameters:
contextMessage- a string that the described the content for theCheckedRunnableand which will be used as message in anyCheckedExceptionRethrownExceptionthrownrunnableThatCanFailWithACheckedException- theCheckedRunnableinstance that will be wrapped as aRunnable- Returns:
- a
Runnablewith aRunnable.run()method that ensures that therun()method is called whenRunnable.run()and any checkedException's thrown will be caught and rethrown as aCheckedExceptionRethrownException.
AnyRuntimeException's thrown aren't caught and the calling code will receive the originalRuntimeExceptionthrown. - Throws:
CheckedExceptionRethrownException- in case therun()method throws a checkedException
-
run
void run() throws Exception
This method performs the operation that may fail with a CheckedException- Throws:
Exception- this method can throw both checkedException's as well asRuntimeException's
-
-