This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Another question from the Sun Certified Java Essentials link. It states:
finally blocks execute no matter what, in other words, whether the code executes without an exception, or an exception is thrown and successfully caught, or an exception is thrown and not caught. This is useful because, except for code in a finally block, if an exception is thrown and not caught, the execution of the rest method is abandoned
I created the program below to test the error process in the finally clause as listed below:
When it runs, I get the Arithmetic error and the "After second error" line doesn't print. Does this mean that if an error is thrown in the finally block and not caught, it would also abandon executing the rest of the code as it does for other try blocks or is it just because I am throwing a deliberate Runtime exception instead of a checked exception? I guess what I am saying is that it appears as if the finally block is subject to the same code abandonment rules as the rest of the try blocks. Does this mean you should be careful with your code in a finally block to make sure you either don't throw any exceptions or catch them in try/catch/finally constructs?
Regardless of where the exception occurs, the JVM will handle it in a consistent manner, so yes, the code in a finally block is subject to the same exception handling logic as any other program block. Naturally, it would not be good programming practice to deeply nest try/catch/finally blocks, so you'll want to limit the amount of logic you place in your finally block -- perform only the essential "clean-up" needed so your program may "gracefully" (hahah, don't we wish), recover.