JMU
Creating Exception Classes in Java
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Review
There's More to Know
The Exception Hierarchy
images/exceptions.gif
The Exception Hierarchy (cont.)
The Throwable Class
The Error Class
The Exception Class
Catching Exceptions Revisited
Catching Exceptions Revisited
Catching Exceptions Revisited (cont.)
Unchecked and Checked Exceptions Revisited
Creating Exception Classes
An Example

Handling Data Format Problems

javaexamples/exceptions/FormatException.java
import java.io.*;

/**
 * An IOException that is thrown when a "reader"
 * encounters an "input stream" that is not formatted properly
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class FormatException extends IOException
{
    /**
     * Default Constructor
     */
    public FormatException()
    {
        this("No details are available");
    }


    
    /**
     * Explicit Value Constructor
     *
     * @param message  Detailed information about the exception
     */
    public FormatException(String message)
    {
        super(message);
    }
}
        
An Example (cont.)
javaexamples/exceptions/AddressTokenizer.java
import java.util.*;

/**
 * A class that can be used for tokenizing strings
 * that contain address information
 *
 * This class assumes that each String contains a name and
 * an email address (with a comma between them)
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class AddressTokenizer
{

    /**
     * Default Constructor
     *
     */
    public AddressTokenizer()
    {
        super();
    }


    /**
     * Tokenize a Sring and return the email address it contains
     *
     * @return   The email address
     */
    public String tokenize(String line) throws FormatException
    {
        FormatException       exception;
        int                   count;
        String                address, name;
        StringTokenizer       tokenizer;


        // Count the tokens
        tokenizer = new StringTokenizer(line,",");
        count     = tokenizer.countTokens();

        // Parse the line (checking the format along the way)
        if (count != 2)
        {
            exception = new FormatException("Missing , in line: "+
                                            line);
            throw exception;
        }
        else
        {
            name    = tokenizer.nextToken();
            address = tokenizer.nextToken();

            if (address.indexOf("@") < 0)
            {
                exception = new FormatException("Missing @ in line: "+
                                                line);
                throw exception;
            }
        }


        return address;
    }
}
        
An Example (cont.)
javaexamples/exceptions/AddressDriver.java
import java.io.*;

/**
 * A class that illustrates the use of the AddressTokenizer
 * and FileFormatException classes
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class AddressDriver
{
    /**
     * The entry point
     *
     * @param args   The command-line arguments
     */
    public static void main(String[] args)
    {
        AddressTokenizer       spammer;
        int                    i;
        String                 email;

        spammer = new AddressTokenizer();

        try {

            for (i=0; i < args.length; i++)
            {
                email = spammer.tokenize(args[i]);

                // Send spam to this email address
            }
        }
        catch (FormatException ffe)
        {
            System.out.println(ffe.getMessage());
        }
    }
}
        
Exceptions and Overriden Methods
Chained Exceptions