JVM | Platform | Status |
---|---|---|
OpenJDK (Temurin) Current | Linux | |
OpenJDK (Temurin) LTS | Linux | |
OpenJDK (Temurin) Current | Windows | |
OpenJDK (Temurin) LTS | Windows |
The jmulticlose
package implements a simple extension to the Java
try-with-resources
statement that allows for closing many resources
at once - without nested or compound statements - in a type-safe manner.
Create a CloseableCollection
to track resources. Add resources to it. The
resources will be closed when the collection is closed. All resources will be closed
even if any of the individual resources raises an exception.
final Resource r0;
final Resource r1;
final Resource r2;
try (CloseableCollectionType<ClosingResourceFailedException> c = CloseableCollection.create()) {
r0 = c.add(new Resource(0));
r1 = c.add(new Resource(1));
r2 = c.add(new Resource(2));
}