Previous | Next | Trail Map | Writing Java Programs | Handling Errors using Exceptions


Java's Catch or Declare Requirement

As stated previously, Java requires that methods either catch or declare all non-runtime exceptions that can be thrown within the scope of that method. This requirement has several components that need further description: "catch", "declare", "non-runtime exceptions", and "the exceptions that can be thrown within the scope of the method".

Catch

A method can catch an exception by providing an exception handler for that type of exception. The next page, Dealing with Exceptions, introduces an example program, talks about catching exceptions, and shows you how to write an exception handler for the example program.

Declare

If a method chooses not to catch an exception, then the method must declare that it can throw it. Why did the Java designers make this requirement? Because any exception that can be thrown by a method is really part of the method's public programming interface: callers of a method must know about the exceptions that a method can throw in order to intelligently and conciously decide what to do about those exceptions. Thus, you declare the exceptions that can be thrown within the scope of a Java methods in the method signature.

The next page, Dealing with Exceptions, talks about declaring exceptions and shows you how to declare exceptions for Java methods that require it.

Non-runtime Exceptions

In Java, there are different types of exceptions, including I/O Exceptions, runtime exceptions and exceptions of your own creation to name a few. Of interest to us in this discussion are runtime exceptions. Runtime exceptions are those exceptions that occur within the Java runtime system. This includes arithmetic exceptions, such as when dividing by zero, pointer exceptions, such as trying to access an object through a null reference, and indexing exceptions, such as attempting to access an array element through an index that is too large or too small.

Runtime exceptions can occur anywhere in a program and in a typical program can be very numerous. Often the cost of checking for runtime exceptions exceeds the benefit of catching or declaring them. Thus the compiler does not require that you catch or declare runtime exceptions, though you can.

Often, this is considered a loophole in Java's exception handling mechanism and programmer's are tempted to declare all exceptions to be runtime exceptions. In general, this is not recommended. Runtime Exceptions--The Controversy contains a thorough discussion about when and how to use runtime exceptions.

The exceptions that can be thrown within the scope of the method

The statement exceptions that can be thrown within the scope of the method may seem obvious at first: just look for the throw statement. However, this statement includes more than just the exceptions that can be thrown directly by the method: the key is in the phrase within the scope of. This phrase includes any exception that can be thrown while the flow of control remains within the method. Thus, this statement includes both


Previous | Next | Trail Map | Writing Java Programs | Handling Errors using Exceptions