- Forward


Creating Exception Classes in Java
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • Exceptions:
    • Provide an alternative way for a method to return control
  • Exception in Java:
    • Exceptions are handled using try and catch
    • Exceptions are thrown using throw
    • Exceptions are declared to be thrown (or re-thrown) using throws
There's More to Know
Back SMYC Forward
  • The Exception Hierarchy
  • Creating Exception Classes
  • Exceptions and Overriden Methods
  • Chained Exceptions
The Exception Hierarchy
Back SMYC Forward
exceptions
The Exception Hierarchy (cont.)
Back SMYC Forward
  • An Important Rationale for Specialization:
    • Reduce code duplication
    • Expand
  • The Additional Rationale Here:
    • Categorization/classification of exceptions
    • Expand
The Throwable Class
Back SMYC Forward
  • Purpose:
    • Only instances of this class (or its descendants) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement
    • Only this class (or its descendants) can be the argument type in a catch clause
  • Capabilities:
    • A snapshot of the execution stack (of its thread) at the time it was created
    • An optional message String
    • An optional cause (i.e., another Throwable that caused it to be thrown)
The Error Class
Back SMYC Forward
  • Purpose:
    • An encapsulation of a serious problem that most applications should not try to handle
  • Examples:
    • LinkageError
    • ThreadDeath
    • VirtualMachineError
The Exception Class
Back SMYC Forward
  • Purpose:
    • An encapsulation of a problem that most applications should handle
  • Examples:
    • AWTException
    • InterruptedException
    • IOException
    • PrintException
    • RuntimeException (which is badly named since they are all are thrown at run-time)
    • ...
Catching Exceptions Revisited
Back SMYC Forward
  • The Situation:
    • Handling hieararchically related exceptions in the same way
  • The Solution
    • The catch 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 }
Catching Exceptions Revisited
Back SMYC Forward
  • The Situation:
    • Handling hierarchically related exceptions differently
  • The Solution:
    • The derived exception must be caught first
  • try { File f = new File("data.txt"); } catch (FileNotFoundException fnfe) { // Derived Class // Take appropriate action } catch (IOException ioe) { // Base Class // Take appropriate action }
Catching Exceptions Revisited (cont.)
Back SMYC Forward
  • The Situation:
    • Handling unrelated exceptions in the same way
  • The Solution:
    • Include a | between classes in the catch clause
  • try { // } catch (IOException | SecurityException e) { // Take appropriate action }
Unchecked and Checked Exceptions Revisited
Back SMYC Forward
  • Unchecked:
    • Specializations of Error or RuntimeException
    • Occur within the Java runtime system (e.g., dividing by zero, pointer exceptions, indexing exceptions)
    • Do not need to be caught or specified (i.e., explicitly re-thrown) but can be
  • Checked:
    • All others
    • Must be caught or specified (i.e., explicitly re-thrown) or a compile-time error will be generated
Creating Exception Classes
Back SMYC Forward
  • When?
    • No existing exceptions are appropriate
    • It would be convenient if I could differentiate an exception from other exceptions
    • You throw several related exceptions and it would be nice to be able to group them (remember, catching a parent will catch all of the descendants)
  • How?
    1. Choose a super class to extend
    2. Override the constructors
An Example
Back SMYC Forward

Handling Data Format Problems

javaexamples/exceptions/FormatException.java
 
An Example (cont.)
Back SMYC Forward
javaexamples/exceptions/AddressTokenizer.java
 
An Example (cont.)
Back SMYC Forward
javaexamples/exceptions/AddressDriver.java
 
Exceptions and Overriden Methods
Back SMYC Forward
  • The Situation:
    • Suppose a method that explicitly throws one or more exceptions is overriden in a derived class
  • Issues:
    • The method in the derived class cannot throw more checked exceptions than the method it is overriding
    • The method in the derived class can throw an exception that is derived from the declared exception (e.g., a EOFException can be thrown when the declared exception is a IOException )
Chained Exceptions
Back SMYC Forward
  • Purpose:
    • Allow you to re-throw an exception and provide additional information (without losing the original cause or stack trace)
  • Adding to the Chain:
    • Use the Throwable(java.lang.Throwable) constructor
    • Use the Throwable.initCause(java.lang.Throwable) method
There's Always More to Learn
Back -