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


A method can catch an exception by providing an exception handler for that type of exception. The section Catching and Handling Exceptions (page 246) introduces 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 the throws clause in the method declaration. The section Specifying the Exceptions Thrown by a Method (page 255) talks about specifying exceptions that a method throws and shows you how to do it.

Checked exceptions

There are two kinds of exceptions: runtime exceptions and nonruntime exceptions. Runtime exceptions occur within the Java runtime system: arithmetic exceptions (such as dividing by zero), pointer exceptions (such as trying to access an object's members through a null reference), and indexing exceptions (such as trying to access an array element with an index that is too large or too small). A method does not have to catch or specify runtime exceptions, although it may.

Nonruntime exceptions are exceptions that occur in code outside of the Java runtime system. For example, exceptions that occur during I/O are nonruntime exceptions. The compiler ensures that nonruntime exceptions are caught or specified; thus, they are also called checked exceptions.

Some consider the fact that you do not have to catch or specify runtime exceptions 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 ExceptionsThe Controversy (page 260) talks about when it's appropriate to use runtime exceptions.

Exceptions that can be thrown by a method

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

Getting Started

Object-Oriented Programming Concepts

Language Basics

Object Basics and Simple Data Objects

Classes and Inheritance

Interfaces and Packages

Handling Errors Using Exceptions

Threads: Doing Two or More Tasks at Once

I/O: Reading and Writing

User Interfaces That Swing

Appendix A. Common Problems and Their Solutions

Appendix B. Internet-Ready Applets

Appendix C. Collections

Appendix D. Deprecated Thread Methods

Appendix E. Reference

The Java Tutorial(c) A Short Course on the Basics
The Java Tutorial: A Short Course on the Basics, 4th Edition
ISBN: 0321334205
EAN: 2147483647
Year: 2002
Pages: 125

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: