What is an exception?
In Java, an exception can be thought of as an error when the
program is being run. There are numerous types of exceptions that may occur.
In order for an exception to be used in a program, it must throw it.
Below is a short example showing a basic exception:
The above example will simply throw a new exception. In general, it is of the form:
where throw is the keyword to generate an exception;
new is creating the Exception object;
ExceptionName is the name of the exception (see below);
and the string "Error Message" is a useful message that the user will see when the exception is
thrown.
Types
In Java, there are many types of exceptions that are defined within the Java libraries.
Below is chart of the most commonly seen exceptions that Java will generate.
| Name |
Description |
| IllegalArgumentException |
An exception to handle a problem with method or command-line arguments.
One way to think of this is if the user does not give a command-line argument
or the argument in a method is not in a certain range, this is the appropriate exception
to be thrown. |
| NumberFormatException |
An exception to handle a problem when formatting a number(s). This is commonly
seen when using any of the parseXXX methods of a certain class. Say that your string is
"1234ff" and you call parseInt; when you get to the 'f', the exception will be thrown since
'f' is not a number. |
| ArrayIndexOutOfBoundsException |
The name says it all. This exception will occur when an index in an array is
out of bounds in either direction (less than 0 or greater than or equal to its size). |
| NullPointerException |
Ah yes, the most commonly seen and most annoying exception of them all! This means that
you are pointing at nothing (or null) perhaps in a linked list or even when dealing with
a JOptionPane. The solution: a bottle of Tylenol and good debugging skills. |
| Exception |
The most general type of exceptions of them all. This will not specifically check for anything
but will mean some kind of error occurred.
|
Try/Catch blocks
In order for you to accurately deal with exceptions, you should use what is called
a try/catch block.
The most general form of the try/catch block is defined below:
where in the above, catch will contain the appropriate code to deal with an Exception. This may be
where to print out the message or contain some other code to try and deal with the error.
ExceptionName is an appropriate exception name and varName is the name of Exception object.
There is also a form of a try/catch/finally block defined below:
What the finally will do is contain some code that whether or not an exception is thrown, that code will
execute. Say that there is an IllegalArgumentException thrown; the code for the catch block will execute
but immediately after that, the finally code will execute. Below is an example using both kinds of
definitions.
Example 1:
Try/Catch example
Download source code here (Right click - Save Tagret As...)
The above program will attempt to add each digit in a numeric string. The best bet is to
use a try catch block, specifically for the NumberFormatException. What will happen is when
the program hits the first non-numeric character, the NumberFormatException will be thrown
from the Integer wrapper class and be caught.
No matter how many times the exception is thrown and caught, he finally code will execute and simply
print the loop counter. Below is the output from the above program:
Let's say that you had more than 1 catch block for the above program. What kind of exception
you catch depends on where you place the catch block. Let's see a modified example of the
above program.
Example 2:
Try/Catch example 2
Download source code here (Right click - Save Target As...)
The output is now:
Because of the newly placed IndexOutOfBoundsException, the NumberFormatException is checked last while
the IndexOutOfBoundsException is checked first. The order of the exceptions is read from top to bottom
in order.
Defining Exceptions
Using the power of inheritance, you can create your own kind of exceptions. Say that you have
a program that relates to both Graduate and Undergraduate students in college. More specifically,
the names of the students are being read from a text file.
Instead of throwing a general IllegalArgumentException which may apply to either the Graduate or
Undergraduate classes. To solve this, you can create a useful exception called
GradStudentException or UnderGradStudentException; both of which are subclasses of the
IllegalArgumentException.
Below is the definition, in general, of your own exceptions:
where in the above, NewExceptionName is your own name for an exception (such as
GradStudentException); OldExceptionName is the name of a current exception (such as
IllegalArgumentException). There is also the constructor for the new exception to allow
a String argument to describe the problem. This is passed to the super class.
Below is a new exception in regard to the student program before:
The file name for each will new exception will be NewExceptionName.java and placed in the same
directory as the main program execution.