![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
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."
A method can catch an exception by providing an exception handler for that type of exception. The section Catching and Handling Exceptionsintroduces an example program, talks about catching exceptions, and shows you how to write an exception handler for it.
A method specifies that it can throw exceptions by using thethrows
clause in the method declaration. The section Specifying the Exceptions Thrown by a Methodtalks about specifying the exceptions that a method throws and shows you how to do it.
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
talks about when it’s appropriate to use runtime exceptions.
The exceptions that a method can throw include
- Any exception thrown directly by the method with the throw statement
- Any exception thrown indirectly by calling another method that throws an exception
![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.