Does a finally block run even if you throw a new Exception?
Yes, the finally block executes, even if a new Exception is thrown within the try or catch blocks. This guarantees actions such as resource release or crucial cleanup operations always run.
Regardless of any exceptions thrown, "Resources released, we're safe!" will be printed, which underscores the sheer resilience of the finally block.
When finally doesn't run: The edge cases
Even though the finally block is impressively reliable, it can't outperform a JVM shutdown. So, if System.exit() is invoked or if the processing thread is unexpectedly interrupted, the finally block might be skipped. Unfortunate hardware issues causing the JVM to crash will also render the finally block useless.
Best practices for utilizing finally
Remember that a finally block is like the janitor of your code. It's designed to clean up resources, not to host hefty pieces of crucial logic. It's like expecting your broom to cook dinner β not going to happen!
Embedding significant logic in the finally block can lead to unreliable behavior β especially dangerous if an uncaught Exception is thrown within it.
Your critical logic should stay cozy and safe outside the try-catch structure, like this:
Also, be careful with trace statements like System.out.println(). Doing in it try-catch can be tricky as it may occasionally disguise the true nature of your program's execution flow:
Re-throwing exceptions: A double-edged sword
Understanding the ripple effect of re-throwing exceptions on your code's control flow is critical. After a re-throw in your catch block, code that follows β including other catch blocksβ stays ignored. How rude! But, the finally block still gets its fair chance to perform its sweeping job:
Variables in finally: A memory exercise
Note that variables set within a finally block remember their value, unless an exception ensues:
This predictable behaviour can carry significant weight when managing application state across exception handling.
Was this article helpful?