Previous | Next | Trail Map | Writing Java Programs | Table of Contents


Handling Errors using Exceptions

If there's one golden rule of programming, it's this: errors occur in software programs. This we know. But what really matters is what happens after the error occurs. How is the error handled? Who handles it? Can the program recover?

The Java language uses exceptions to provide error handling capabilities for its programs. In this lesson you will learn what an exception is, how to throw and catch exceptions, what to do with an exception once you've caught it, and how to best use the exception class hierarchy provided by the Java development environment.

Note on terminology: Some languages (or their development environments) use the terms raise and handle the same way that Java uses throw and catch. Java inherits its terminology and much of its exception syntax from C++.

What's an Exception and Why Do I Care?

An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.

Your First Encounter with Java Exceptions

Now that you understand what exceptions are and the advantages of using exceptions in your Java programs, let's start writing some Java code to put exceptions to use.

If you have done any amount of Java programming at all, you have undoubtedly seen an error message similar to this one:

FileWithGetLine.java:8: Warning: Exception java.io.FileNotFoundException must be caught, or it must be declared in throws clause of this method.
	fis = new FileInputStream(filename);
	      ^
This message indicates that the compiler found an exception that is not being dealt with. The Java language requires that all non-runtime exceptions be caught or declared. At first, this may seem overburdensome to you, however, there are advantages to this requirement.

Java's Catch or Declare Requirement

Java requires that your programs either catch or declare all non-runtime exceptions that can be thrown within the scope of that method.

Dealing with Exceptions

This page introduces an example that we will use to show you how to catch an exception and handle it, and alternatively, how to declare an exception that can be thrown within a method.

Throwing Exceptions

So far, you've been catching exceptions thrown by the Java runtime system or classes from the Java packages. This section shows you how you can throw exceptions from your own Java code using the throw statement. All exceptions must be Throwable, that is, exceptions must inherit (either directly or indirectly) from the Throwable class defined in the java.lang package.

Runtime Exceptions--The Controversy

Because the Java language does not require methods to catch or declare runtime exceptions, it's tempting for programmers to write code that always throw runtime exceptions, or make all of their exception subclasses inherit from RuntimeException. Both of these are "exception abuse" and are not recommended.

Note to C++ Programmers: Java exception handlers can have a finally block which allows programs to cleanup after the try block. See The finally Block for more information about how to use the finally statement.


Previous | Next | Trail Map | Writing Java Programs | Table of Contents