Starts loaning an auto-closeable resource to a function.
Starts loaning an auto-closeable resource to a function.
Usage example:
Loan(new PrintWriter(new FileOutputStream("hello-world.txt"))) { w: PrintWriter => w.println("Hello world!") }
In this example, w.close()
is guaranteed to get called even if the to-function terminates with a Throwable.
In general, if the to-function throws a throwable t1
and the AutoCloseable.close()
method throws another
throwable t2
, then the throwable t2
gets added to the throwable t1
using t1.addSuppressed(t2)
.
the type of the auto-closeable resource.
the auto-closeable resource.