Discussion
Once you throw an exception, you normally have two choices––catch it or let it terminate the thread. But this program shows a third possibility, with an exception that just vanishes into thin air.
The strange behavior is enabled by the finally block, which is guaranteed to run before the try section exits. Since the finally block is the very last thing to happen, a jump instruction such as return or throw inside it will supersede everything that came before.
Guaranteed to Run
One important use for a try–finally block is to manage files and network connections. Your operating system can only deal with so many open connections or files at a time, so it’s vital to close the ones you’ve finished with to ensure you can open new ones later. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access