Exception Objects
Here is how the try/catch structure looks:
try
{
// statements, some of which might throw an exception
}
catch ( SomeExceptionType ex )
{
// statements to handle this type of exception
}
.... // more catch blocks
When a catch{} block receives control (ie. starts execution) it
will have a reference to an object of class Exception (or a subclass of Exception).
The specific class of the object depends on what exception was thrown.
An exception will be caught if its class or an ancestor class is
in the list of catch{} blocks.
When an exception condition arises,
the Java run time system takes over for a while,
creates an exception object and puts information in it.
If the exception arose inside a try{} block,
it will be caught by an appropriate catch{} block.
Exception objects have several member methods,
including:
- public void printStackTrace()
- Print a stack trace --- a list that shows the sequence of
method calls up to this exception.
- public String getMessage()
- Return a string that may describe what went wrong.
A catch{} block can use these methods
to write an informative error message to the monitor
without terminating the program.
|