Class 2 |
Intermediate Java 30-IT-397 |
|
Throwing Exceptions
-
Java allows you to have an alternate exit path if a method malfunctions.
-
Instead of simply returning a number or other return value that represents
an error code, you can send back more information.
-
The alternate path throws an exception. An Exception
is an object that encapsulates error information.
-
When an exception is thrown, the program skips all code until it finds
an exception handler that is built to deal with that exception.
-
There are different types of Exceptions, each of which extends class Exception.
-
Exception extends class Throwable.
-
IOException, ParseException, etc. These are all linked on the Exception
API documentation.
-
You can even create your own Exception subclasses. Used properly,
these can be very powerful!
-
Each exception type can have its own handler.
-
On a high level, we can group exceptions into two types:
-
Those that extend RuntimeException
and those that do not.
-
Generally, RuntimeExceptions are errors that occur due to programming problems,
such as bad casts, trying to reach an array element that does not exist,
and null pointers..
-
Other errors are not controlled by the program, such as an IO error.
These include attempting to read past the end of a file, attempting to
open an incorrect URL, and trying to instantiate a class dynamically that
does not exist.
-
Class Error also extends Throwable, but these are generally internal
errors. You shouldn't throw or catch these.
Informing Others

Created by: Brandan Jones
December 17, 2001