public final class Closer
extends java.lang.Object
implements java.io.Closeable
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes all
Closeable instances that have been added to this Closer . |
static Closer |
create()
Creates a new
Closer . |
<C extends java.io.Closeable> |
register(C closeable)
|
<Z extends java.util.zip.ZipFile> |
register(Z zipFile)
|
java.lang.RuntimeException |
rethrow(java.lang.Throwable e)
Stores the given throwable and rethrows it.
|
<X extends java.lang.Exception> |
rethrow(java.lang.Throwable e,
java.lang.Class<X> declaredType)
Stores the given throwable and rethrows it.
|
<X1 extends java.lang.Exception,X2 extends java.lang.Exception> |
rethrow(java.lang.Throwable e,
java.lang.Class<X1> declaredType1,
java.lang.Class<X2> declaredType2)
Stores the given throwable and rethrows it.
|
java.lang.RuntimeException |
rethrowAndClose(java.lang.Throwable t)
Call
rethrow(Throwable) with the given exception, but before throwing the exception,
also close this Closer. |
public <C extends java.io.Closeable> C register(C closeable)
closeable
public <Z extends java.util.zip.ZipFile> Z register(Z zipFile)
closeable
public java.lang.RuntimeException rethrowAndClose(java.lang.Throwable t) throws java.io.IOException
rethrow(Throwable)
with the given exception, but before throwing the exception,
also close this Closer. Exceptions from closing are added to t
as suppressed
exceptions.t
- the throwable that should be re-thrownjava.io.IOException
- if t
is an IOException, or one occurspublic java.lang.RuntimeException rethrow(java.lang.Throwable e) throws java.io.IOException
IOException
, RuntimeException
or Error
. Otherwise, it will be rethrown
wrapped in a RuntimeException
. Note: Be sure to declare all of the checked
exception types your try block can throw when calling an overload of this method so as to avoid
losing the original exception type.
This method always throws, and as such should be called as
throw closer.rethrow(e);
to ensure the compiler knows that it will throw.
java.io.IOException
- when the given throwable is an IOExceptionpublic <X extends java.lang.Exception> java.lang.RuntimeException rethrow(java.lang.Throwable e, java.lang.Class<X> declaredType) throws java.io.IOException, X extends java.lang.Exception
IOException
, RuntimeException
, Error
or a checked exception of the
given type. Otherwise, it will be rethrown wrapped in a RuntimeException
. Note:
Be sure to declare all of the checked exception types your try block can throw when calling an
overload of this method so as to avoid losing the original exception type.
This method always throws, and as such should be called as
throw closer.rethrow(e, ...);
to ensure the compiler knows that it will throw.
java.io.IOException
- when the given throwable is an IOExceptionX
- when the given throwable is of the declared type XX extends java.lang.Exception
public <X1 extends java.lang.Exception,X2 extends java.lang.Exception> java.lang.RuntimeException rethrow(java.lang.Throwable e, java.lang.Class<X1> declaredType1, java.lang.Class<X2> declaredType2) throws java.io.IOException, X1 extends java.lang.Exception, X2 extends java.lang.Exception
IOException
, RuntimeException
, Error
or a checked exception of either
of the given types. Otherwise, it will be rethrown wrapped in a RuntimeException
.
Note: Be sure to declare all of the checked exception types your try block can throw
when calling an overload of this method so as to avoid losing the original exception type.
This method always throws, and as such should be called as
throw closer.rethrow(e, ...);
to ensure the compiler knows that it will throw.
java.io.IOException
- when the given throwable is an IOExceptionX1
- when the given throwable is of the declared type X1X2
- when the given throwable is of the declared type X2X1 extends java.lang.Exception
public void close() throws java.io.IOException
Closeable
instances that have been added to this Closer
. If an
exception was thrown in the try block and passed to one of the exceptionThrown
methods,
any exceptions thrown when attempting to close a closeable will be suppressed. Otherwise, the
first exception to be thrown from an attempt to close a closeable will be thrown and any
additional exceptions that are thrown after that will be suppressed.close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException