Creating Exception Classes in Java
An Introduction |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
try
and catch
throw
throws
Throwable
Classthrow
statementString
Throwable
that caused it to be thrown)Error
ClassException
Classcatch
clause will be applied to
any exception or specialization of exception
(so you can catch multiple exceptions in one block
if they are derived from the same class)
try { // } catch (IOException ioe) { // FileNotFoundException, EOFException // Take appropriate action }
try { File f = new File("data.txt"); } catch (FileNotFoundException fnfe) { // Derived Class // Take appropriate action } catch (IOException ioe) { // Base Class // Take appropriate action }
|
between classes in
the catch
clausetry { // } catch (IOException | SecurityException e) { // Take appropriate action }
Error
or
RuntimeException
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); } }
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; } }
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()); } } }