Exceptions

Handling Errors with Exceptions Sun tutorial. A link to the older version
Has nice links accessible from each of the headers below. Make sure to look into these links.
API

Java provides exception handling.

"What Is an Exception and older "What's an Exception and Why Do I Care?"

Exceptions are signals that indicate that some sort of exceptional condition (such as an error) has taken place. When an error occurs in your program, the code finds the error and can throw an exception. You can catch the thrown exception and take actions to recover from it.

An exception is an object that is an instance of some subclass of java.lang.Throwable. Throwable has two standard subclasses java.lang.Error and java.lang.Exception

"Your First Encounter with Java Exceptions"

If you have done any amount of Java programming at all, you have undoubtedly already encountered exceptions. Your first encounter with Java exceptions was probably in the form of an error message from the compiler like this one:
InputFile.java:11: Exception java.io.FileNotFoundException
must be caught, or it must be declared in the throws clause
of this method.
        in = new FileReader(filename);
             ^
This message indicates that the compiler found an exception that is not being handled. The Java language requires that a method either catch all "checked" exceptions (those that are checked by the runtime system) or specify that it can throw that type of exception.

For standard java provided classes, one can see when a method throws an exception (and which) by its definition

public static native void sleep(long millis) throws InterruptedException

"Java's Catch or Specify Requirement"

Exceptions propogate up through the lexical block structure of a Java method, and then up the method call stack. If an exception is not caught by the block of code that throw it, it propogates to the next higher enclosing block of code. If it is not caught there, it propogates all the way to the main() method and then causes the Java interpreter to print an error message and a stack trace and exit.

The idea is to concentrate on the errors that can happen within contexts (i.e., handle it within the specific algorithm).

"Dealing with Exceptions", local

The main section in this is "Catching and Handling Exceptions" - I like the organization of the old version better

In brief:

try

The try clause simply establishes a block of code that is to have its exceptions and abnormal exits (through break, continue, return or exception propogation) handled. The try clause by itself does not do anything interesting; it is the catch and finally clauses that do exception handling and clean- up operations

catch

A try block may be followed by zero or more catch clauses that specify code to handle various types of exceptions. (A try statement must be accompanied by at least one catch block or one finally block.) catch clauses have an unusual syntax: each is declared with an argument, much like a method argument. This argument must be of the type Throwable or a subclass.

When an exception occurs, the first catch clause that has an argument of the appropriate type is invoked. The type of the argument must match the type of the exception object, or it must be a subclass of the exception. This catch argument is valid only within the catch block, and refers to the actual exception object that was thrown.

Here is an example illustrating the exception hierarchy test

finally

The finally clause is generally used to clean up after the try clause. What is useful in the finally, is that the code in a finally is guaranteed to be executed, if any portion of the try block is executed, regardless of how the code in the try block completes.

Normal: control reaches end of try and then does finally. If control reaches try because of a return, continue, or break, finally is executed before control transfers to its new destination. If an exception occurs in the try block and there is a local catch, control goes first to the catch and then to finally. If there is not a local catch, control transfers first to the finally, then propogates to the nearest catch

Java provides several predefined exceptions:

Common exceptions: these are from java.lang

Examples of use (silly example of bad code through-out ) What happens? A try block (even one command call - check out the reflection package java.lang.reflect.Method invoke()) could cause more than one type of exception

Actually both of these are stupid - use %

Make it yourself exception handling:

"Throwing Exceptions"

An example:

Suppose you have a client/server program. In the client code, you try to connect to the server, and expect a response within 5 seconds.

If you want to catch your exception, you need to use try

"Runtime Exceptions--The Controversy"