-
- All Superinterfaces:
-
AutoCloseable
- 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 CloseableScope extends AutoCloseable
Specialization ofAutoCloseablethat throws only unchecked exceptions. It is intended to be used with try-with-resources construct to implement custom scoping rules.- See Also:
- CloseableScope tutorial
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default CloseableScopeandFinally(Runnable action)Creates extendedCloseableScopethat additionally runs specifiedactionregardless of exceptions.default CloseableScopeandThen(Runnable action)Creates extendedCloseableScopethat additionally runs specifiedactionunless exception is thrown.voidclose()Closes this scope or other closeable resource.
-
-
-
Method Detail
-
close
void close()
Closes this scope or other closeable resource. This is a specialization ofAutoCloseable.close()that throws only unchecked exceptions.- Specified by:
-
closein interfaceAutoCloseable
-
andThen
default CloseableScope andThen(Runnable action)
Creates extendedCloseableScopethat additionally runs specifiedactionunless exception is thrown. This is useful for quickly creating outer scopes that add extra operations to some inner scope. Ifclose()throws,actiondoes not run.- Parameters:
-
action- the operation to perform afterclose()is called - Returns:
-
new
CloseableScopethat, when closed, first closes thisCloseableScopeand then executesaction - Throws:
-
NullPointerException- ifactionisnull - See Also:
-
andFinally(Runnable)
-
andFinally
default CloseableScope andFinally(Runnable action)
Creates extendedCloseableScopethat additionally runs specifiedactionregardless of exceptions. This is useful for quickly creating outer scopes that add extra operations to some inner scope. Ifclose()throws,actionruns anyway as if in try-with-resources block. If bothclose()} andactionthrow, the exception fromactionis added to suppressed exception list by callingThrowable.addSuppressed(Throwable).- Parameters:
-
action- the operation to perform afterclose()is called - Returns:
-
new
CloseableScopethat, when closed, first closes thisCloseableScopeand then executesaction - Throws:
-
NullPointerException- ifactionisnull - See Also:
-
andThen(Runnable)
-
-