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.
-
Method Summary
Modifier and TypeMethodDescriptionvoidrun()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 Details
-
run
This method performs the operation that may fail with a CheckedException- Throws:
Exception- this method can throw both checkedException's as well asRuntimeException's