EXCEPTIONS
(Top)
The following diagram shows the basic exception hierarchy:
The classes in the exception hierarchy are:
The NumberFormatException, a subclass of RuntimeException is thrown by methods that convert strings to numeric data types. If you are interacting with a user, it may be worthwhile to catch this exception.
Nothing prevents you from throwing a RuntimeException and since they do not need to be declared in the throws clause, some programmers will use this type of exception to avoid the work of handling exceptions properly. But, this is not good programming practice.
Object
|
+-- Throwable
|
|-- Error
| |
| ...
|
+-- Exception
|
|-- java.awt.AWTException
|
|-- ClassNotFoundException
|
|-- CloneNotSupportedException
|
|-- IllegalAccessException
|
|-- InstantiationException
|
|-- InterruptedException
|
|-- java.io.IOException
| |
| |-- EOFException
| |
| |-- FileNotFoundException
| |
| |-- InterruptedIOException
| |
| |-- UTFDataFormatException
| |
| |-- java.net.MalformedURLException
| |
| |-- java.net.ProtocolException
| |
| |-- java.net.SocketException
| |
| |-- java.net.UnknownHostException
| |
| +-- java.net.UnknownServiceException
|
+-- RuntimeException
|
|-- Arithmetic Exception
|
|-- ArrayStoreException
|
|-- ClassCastException
|
|-- IllegalArgumentException
| |
| |-- IllegalThreadStateException
| |
| +-- NumberFormatException
|
|-- IndexOutOfBoundsException
| |
| |-- ArrayIndexOutOfBoundsException
| |
| +-- StringIndexOutOfBoundsException
| ...
CATCHING EXCEPTIONS
(Top)
Exceptions are passed up through the stack of invoking methods until they are caught. The catcher of last resort is the Java virtual runtime that will display the exception and dump a stack trace for any exception that is not caught.
The following illustrates a version of the standard main method that is sometimes useful when you want to test a program that can generate various types of errors:
public class TestProg
{
private static void execute() throws ....
{
// --- Insert test code here
}
public static void main(String[] args)
{
//Display init message
System.out.println("-- INIT TEST NAME");
//Run until user says stop
boolean run = true;
while (run)
{
///Try to execute
/*1*/ try
{
execute();
} //try
//Catch all exceptions
/*2*/ catch (Exception error)
{
//Rethrow runtime exceptions
/*3*/ if (error instanceof RuntimeException)
throw (RuntimeException)error;
//Display other exceptions
/*4*/ System.out.println("** EXCEPTION DETECTED");
System.out.println(" " + error.getMessage());
error.printStackTrace(System.out);
} //catch
//Ask if want to run again
try
{
/*5*/ System.out.println();
System.out.print("Type y to execute again: ");
System.out.flush();
int c = System.in.read();
run = (c == 'y') || (c == 'Y');
/*6*/ while (System.in.read() != 'n');
System.out.println();
}
catch (IOException error)
{
System.out.println();
System.out.println("** IO EXCEPTION GETTING YOUR ANSWER");
System.out.println(" " + error.getMessage());
run = false;
}
} //while
//Display term message
System.out.println("-- TERM TEST NAME");
}
}
------------
The source code for this main method is included in a simple exception test program. You may want to play with your own copy.
CUSTOM EXCEPTIONS
(Top)
A programmer may choose to create new Exception subclasses for several reasons.
public class MyDescriptiveException extends Exception
{
public MyDescriptiveException()
{
super();
}
public MyDescriptiveException(String message)
{
super(message);
}
}
public class MyRestrictiveException extends Exception
{
public MyRestrictiveException(String message)
{
super(message);
}
}
public class MyObjectException extends Exception
{
private MyObject theSource;
public MyObjectException(MyObject source, String message)
{
super(message);
theSource = source;
}
public MyObject getSource()
{
return(theSource);
}
}
Now, this exception can be thrown by anyone, including an instance
of MyObject as the following illustrates.
public class MyObject
{
//Size limit constants
private static final int minSize = 1;
private static final int maxSize = 100;
//Instance variables
private int theSize;
public MyObject(int size) throws MyObjectException
{
//Save size
theSize = size;
//Validate size
if (size < minSize)
throw new MyObjectException(this,"Size below minimum");
if (size > maxSize)
throw new MyObjectException(this,"Size above maximum");
}
public int getSize()
{
return(theSize);
}
.....
}
Notice that the object instance that is passed to the new exception
is this object.
Prepared by David L. March -- Last Revised on September 22, 1998
COPYRIGHT © 1998 BY DAVID L. MARCH