The try and catch blocks

In any block of code where we think an exception might be thrown and we'd like to handle the exception, we're going to wrap that line of code in a try block. For the most part, this doesn't affect how this code is executed unless an exception occurs within the try block. If an exception is thrown within the try block, instead of propagating that exception upward to the next level, the code within the following catch block will immediately get executed.

Note that catch blocks require a little more information before they can execute; they need to know what exactly they're going to catch. We can catch all exceptions by simply catching anything of the Exception class, but this may not be a fair thing to do. There's a ...

Get Java Programming for Beginners now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.