O'Reilly logo

Java Programming for Beginners by Mark Lassoff

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required