Exception Handling
We've already seen some examples of exception handling in previous lessons.
This might bring back some memories for you:
try{
output = Integer.parseInt(input);
}catch(NumberFormatException nfe){
output = -555; //error code
}
In this example we try to get output from the Integer.parseInt method. It
takes a string as an argument, but it only works if the string represents an
integer value. If the string does not represent an integer value, then an
exception is thrown and the catch clause is triggered. In some ways this
construct is similar to an if-then statement.
Here's a much more interesting example:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class evenNumber extends Applet{
String input, output;
CheckNumber cn;
public void init(){
setBackground(Color.yellow);
cn = new CheckNumber();
input = "";
output = "";
requestFocus();
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent k) {
if(k.getKeyCode() == KeyEvent.VK_ENTER){
try{
int n = Integer.parseInt(input);
try{
cn.check_number(n);
output="even";
} catch(EvenNumberException ene){
output = "odd";
}
}catch(NumberFormatException nfe){
output = "Not Integer Value";
}
}
else if(k.getKeyCode() == KeyEvent.VK_DELETE){
input = "";
output = "";
}
else if(k.getKeyCode() == KeyEvent.VK_BACK_SPACE &&
input.length() > 0 ){
input = input.substring(0,input.length()-1);
output = "";
}
else if(k.getKeyCode() != KeyEvent.VK_SHIFT){
input += k.getKeyChar();
}
repaint();
}
});
}
public void paint(Graphics g){
g.setColor(Color.black);
g.drawString("Even Numbers", 10, 20);
g.drawString("INPUT: "+input, 10, 40);
g.drawString("OUTPUT: "+output, 10, 60);
}
}
class EvenNumberException extends Exception { }
class CheckNumber {
public void check_number(int x) throws EvenNumberException{
if(x%2!=0) throw new EvenNumberException();
}
}
//
In this example we actually create our own custom made exception. Our custom
made exception is called EvenNumberException. Not only do we create this
exception, we also throw it. In the CheckNumber class we have a method which
throws this exception any time it encounters an odd number. In the applet
class we have an embedded try-catch construct which you should study
carefully. Make sure you understand how each of the three possible output
messages are triggered.
There is a lot to learn about exceptions and this lesson only touches on
the basics, but before we get to the lesson you should at least see a fully
formed try-catch-finally construct:
try {
// The guarded region: Dangerous activities
// that might throw A, B, or C
} catch( A a1 ) {
// Handler for situation A
} catch( B b1 ) {
// Handler for situation B
} catch( C c1 ) {
// Handler for situation C
} finally {
// Activities that happen every time
}
This example was taken from the book, Thinking In Java by Bruce
Eckel. See Bruce Eckel for more
information on his book.
----------------------------------------------------------
Assignment:
Create your own classes which extend Exception and methods which throw an
instance of each of your custom exceptions. The applet which will use your custom
exception will sort words beginning with a vowel from words that don't
begin with a vowel. The user will input a word and press enter. The applet
will report back whether the word begins with a vowel, a consonant, or a
non-letter. Exceptions must be an integral part of your approach to
designing this applet. Name your exceptions appropriately. Hint: You will
need to embed your exceptions.