Exception matching

Bruce Eckel's Thinking in Java Contents | Prev | Next

//: Human.java
// Catching Exception Hierarchies
 
class Annoyance extends Exception {}
class Sneeze extends Annoyance {}
 
public class Human {
  public static void main(String[] args) {
    try {
      throw new Sneeze();
    } catch(Sneeze s) {
      System.out.println("Caught Sneeze");
    } catch(Annoyance a) {
      System.out.println("Caught Annoyance");
    }
  }
} ///:~ 

The Sneeze exception will be caught by the first catch clause that it matches, which is the first one, of course. However, if you remove the first catch clause:

    try {
      throw new Sneeze();
    } catch(Annoyance a) {
      System.out.println("Caught Annoyance");
    }

The remaining catch clause will still work because it’s catching the base class of Sneeze. Put another way, catch(Annoyance e) will catch a Annoyance or any class derived from it . This is useful because if you decide to add more exceptions to a method, if they’re all inherited from the same base class then the client programmer’s code will not need changing, assuming they catch the base class, at the very least.

If you try to “mask” the derived-class exceptions by putting the base-class catch clause first, like this:

    try {
      throw new Sneeze();
    } catch(Annoyance a) {
      System.out.println("Caught Annoyance");
    } catch(Sneeze s) {
      System.out.println("Caught Sneeze");
    }

the compiler will give you an error message, since it sees that the Sneeze catch-clause can never be reached.

Exception guidelines

  1. Fix the problem and call the method (which caused the exception) again.
  2. Patch things up and continue without retrying the method.
  3. Calculate some alternative result instead of what the method was supposed to produce.
  4. Do whatever you can in the current context and rethrow the same exception to a higher context.
  5. Do whatever you can in the current context and throw a different exception to a higher context.
  6. Terminate the program.
  7. Simplify. If your exception scheme makes things more complicated, then it is painful and annoying to use.
  8. Make your library and program safer. This is a short-term investment (for debugging) and a long-term investment (for application robustness).


Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds