Managing Exceptions

Now that you know what an exception is, how do you deal with one in your own code? In many cases, the Java compiler enforces exception management when you try to use methods that use exceptions; you need to deal with those exceptions in your own code or it simply won't compile. In this section, you learn about consistency checking and how to use the try, catch, and finally language keywords to deal with exceptions that might occur.

Exception Consistency Checking

The more you work with the Java class libraries, the more likely it is that you'll run into a compiler error (an exception!) similar to this one:

 XMLParser.java:32: Exception java.lang.InterruptedException must be caught or it must be declared in the throws clause of ...

Get Sams Teach Yourself Java 2 in 21 Days, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.