The finally block
If we'd like to execute a case always, no matter whether we did or did not succeed in our try block, we can follow our catch block with the finally block. The finally block executes no matter what, whether an exception was or was not caught. This exists so that you can put cleanup code in your system. An example of cleanup code is clearing out our reader objects buffer so that there's nothing there to confuse ourselves later or other programmers.
Exceptions are more than a simple object that gets thrown; they can contain a lot of really important information. As we saw earlier, exceptions can contain a stack trace. Let's quickly modify our program so that while it still gives user-friendly Please enter a float number. information, ...
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