Catching any exception
It is possible to create a handler that catches any type of exception. You do this by catching the base-class exception type Exception (there are other types of base exceptions, but Exception is the base thats pertinent to virtually all programming activities):
catch(Exception e) {
System.err.println("Caught an exception");
}
This will catch any exception, so if you use it youll want to put it at the end of your list of handlers to avoid preempting any exception handlers that might otherwise follow it.
Since the Exception class is the base of all the exception classes that are important to the programmer, you dont get much specific information about the exception, but you can call the methods that come from its base type Throwable:
String getMessage( )
String getLocalizedMessage( )
Gets the detail message, or a message adjusted for this particular locale.
String toString( )
Returns a short description of the Throwable, including the detail message if there is one.
void printStackTrace( )
void printStackTrace(PrintStream)
void printStackTrace(java.io.PrintWriter)
Prints the Throwable and the Throwables call stack trace. The call stack shows the sequence of method calls that brought you to the point at which the exception was thrown. The first version prints to standard error, the second and third prints to a stream of your choice (in Chapter 12, youll understand why there are two types of streams).
Throwable fillInStackTrace( )
Records information within this Throwable object about the current state of the stack frames. Useful when an application is rethrowing an error or exception (more about this shortly).
In addition, you get some other methods from Throwables base type Object (everybodys base type). The one that might come in handy for exceptions is getClass( ), which returns an object representing the class of this object. You can in turn query this Class object for its name with getName( ). You can also do more sophisticated things with Class objects that arent necessary in exception handling.
Heres an example that shows the use of the basic Exception methods:
//: c09:ExceptionMethods.java
// Demonstrating the Exception Methods.
import com.bruceeckel.simpletest.*;
public class ExceptionMethods {
private static Test monitor = new Test();
public static void main(String[] args) {
try {
throw new Exception("My Exception");
} catch(Exception e) {
System.err.println("Caught Exception");
System.err.println("getMessage():" + e.getMessage());
System.err.println("getLocalizedMessage():" +
e.getLocalizedMessage());
System.err.println("toString():" + e);
System.err.println("printStackTrace():");
e.printStackTrace();
}
monitor.expect(new String[] {
"Caught Exception",
"getMessage():My Exception",
"getLocalizedMessage():My Exception",
"toString():java.lang.Exception: My Exception",
"printStackTrace():",
"java.lang.Exception: My Exception",
"%% \tat ExceptionMethods.main\\(.*\\)"
});
}
} ///:~
You can see that the methods provide successively more informationeach is effectively a superset of the previous one.