|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object ix.util.WithCleanup
public abstract class WithCleanup
A class that encapsulates the tricks needed to use a "finally" clause that might throw an exception. A throw (or return) out of a "finally" clause will cause any exception from the "try" clause to be lost. So there has to be another try-catch inside the "finally", and up to two exceptions might have to be handled.
Instead of creating a plain Runnable, like this:
new Runnable() { public void run() { ... do something ... } }you can instead create a WithCleanup:
new WithCleanup() { public void body() { ... do something ... } public void cleanup() { ... do some cleanup ... } }
When the WithCleanup's run()
method is called,
it will call body()
inside a "try", and cleanup()
inside the corresponding "finally".
If any exception (any Throwable) is thrown out of the body or
cleanup, this method throws a RethrownException
. If only
the body, or only the cleanup, throws an exception, then the
RethrownException is a simple wrapper around that exception.
If both body and cleanup throw an exception, the RethrownException
is wrapped around the body's exception, but its message describes
both exceptions. In addition, Debug.describeException(Throwable)
is called on cleanup's exception to print its backtrace.
Constructor Summary | |
---|---|
WithCleanup()
|
Method Summary | |
---|---|
abstract void |
body()
The main part of what this object should do when its run() method is called. |
abstract void |
cleanup()
Whatever this object should always do after its body() method has returned. |
void |
run()
Calls body() in a "try", cleanup()
in the corresponding "finally", and ensures that any
exception thrown by body() is rethrown. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public WithCleanup()
Method Detail |
---|
public void run()
body()
in a "try", cleanup()
in the corresponding "finally", and ensures that any
exception thrown by body()
is rethrown.
See the class's javadoc for a more complete explanation.
Subclasses should not normally redefine this method.
run
in interface java.lang.Runnable
public abstract void body() throws java.lang.Exception
run()
method is called.
java.lang.Exception
public abstract void cleanup() throws java.lang.Exception
body()
method has returned. cleanup() is
called in a "finally" that goes with a "try"-"catch"
that's around the call to body()
.
java.lang.Exception
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |