public final class Closer extends Object implements 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 Closeable> |
register(C closeable)
|
RuntimeException |
rethrow(Throwable e)
Stores the given throwable and rethrows it.
|
<X extends Exception> |
rethrow(Throwable e,
Class<X> declaredType)
Stores the given throwable and rethrows it.
|
<X1 extends Exception,X2 extends Exception> |
rethrow(Throwable e,
Class<X1> declaredType1,
Class<X2> declaredType2)
Stores the given throwable and rethrows it.
|
public <C extends Closeable> C register(C closeable)
closeablepublic RuntimeException rethrow(Throwable e) throws 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.
IOException - when the given throwable is an IOExceptionpublic <X extends Exception> RuntimeException rethrow(Throwable e, Class<X> declaredType) throws IOException, X extends 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.
IOException - when the given throwable is an IOExceptionX - when the given throwable is of the declared type XX extends Exceptionpublic <X1 extends Exception,X2 extends Exception> RuntimeException rethrow(Throwable e, Class<X1> declaredType1, Class<X2> declaredType2) throws IOException, X1 extends Exception, X2 extends 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.
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 Exceptionpublic void close()
throws 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 Closeableclose in interface AutoCloseableIOExceptionCopyright © 2010-2014. All Rights Reserved.