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