The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Essential Java Classes
Lesson: Handling Errors with Exceptions

The Catch or Specify Requirement

The Java runtime system requires that a method must either catch or specify all checked exceptions that can be thrown by that method. This requirement has several components that need further description: "catch," "specify," "checked exceptions," and "exceptions that can be thrown by that method."

Catch

A method can catch an exception by providing an exception handler for that type of exception. The section Catching and Handling Exceptions (in the Essential Java Classes trail) introduces an example program, talks about catching exceptions, and shows you how to write an exception handler for it.

Specify

A method specifies that it can throw exceptions by using the throws clause in the method declaration. The section Specifying the Exceptions Thrown by a Method (in the Essential Java Classes trail) talks about specifying the exceptions that a method throws and shows you how to do it.

Checked exceptions

As mentioned previously, there are two kinds of exceptions: checked and unchecked exceptions. The compiler ensures that checked exceptions are caught or specified.

Some consider the fact that you do not have to catch or specify runtime exceptions or errors a loophole in the exception-handling mechanism. Many programmers are tempted to use runtime exceptions instead of checked exceptions so that they donít have to catch or specify them. In general, this is not recommended. The section Runtime Exceptions—The Controversy (in the Essential Java Classes trail) talks about when itís appropriate to use runtime exceptions.

Exceptions that can be thrown within the scope of the method

The exceptions that a method can throw include

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.