Wouldn't it be better if the method is written to always guarantee to leave the object
in a well defined state?
If the object is not left in a well defined state then even if you catch Throwable, then
there's not much you can do since you don't know if the underlying resource (or
whatever) was actually allocated.
E.g.
| Connection getConnection() throws Throwable
| {
|
| log.info("before allocation");
|
| Connection conn = ......
|
| log.info("after allocation");
|
| return conn;
| }
|
In the above, both calls to log.info can throw a RuntimeException. If it's thrown from
the first log.info line then the connection hasn't been allocated, but if it's
thrown from the second one then the connection has been allocated.
If you do something like:
| Connection getConnection()
| {
| Connection conn = null;
|
| try
| {
|
| log.info("before allocation");
|
| conn = ......
|
| log.info("after allocation");
| }
| catch (Throwable t)
| {
| if (conn != null) conn.close();
|
| throw new IllegalStateException();
| }
|
| return conn;
| }
|
|
Then your're not leaving anything to chance and you can avoid throws Throwable.
This is mentioned in Bloch's effective Java "Strive for failure atomicity"
What am I missing?
View the original post :
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3961343#...
Reply to the post :
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&a...