March 1, 2021
Hot Topics:

Object Integrity & Security: Error & Exceptions

  • By Matt Weisfeld
  • Send Email »
  • More Articles »


This is the thirty-eighth installment in a series of articles about fundamental object-oriented (O-O) concepts and related object-oriented technologies. The material presented in these articles is based on material from the second edition of my book, The Object-Oriented Thought Process, 2nd edition. The Object-Oriented Thought Process is intended for anyone who needs to understand basic object-oriented concepts and technologies before jumping directly into the code. Click here to start at the beginning of the series.

In keeping with the code examples used in the previous articles, Java will be the language used to implement the concepts in code. One of the reasons that I like to use Java is because you can download the Java compiler for personal use at the Sun Microsystems Web site http://java.sun.com/. You can download the standard edition, J2SE 5.0, at http://java.sun.com/j2se/1.5.0/download.jsp to compile and execute these applications. I often reference the Java J2SE 5.0 API documentation and I recommend that you explore the Java API further. Code listings are provided for all examples in this article as well as figures and output (when appropriate). See the first article in this series for detailed descriptions for compiling and running all the code examples (http://www.developer.com/design/article.php/3304881).

The code examples in this series are meant to be a hands-on experience. There are many code listings and figures of the output produced from these code examples. Please boot up your computer and run these exercises as you read through the text.

For the past several months, you have been exploring various issues regarding object integrity, security, and performance. In last month's article, you covered some of the basic techniques for handling anomalies. Object-oriented languages generally use exception-handling models that facilitate the handling of these types of anomalies.

In this month's article, you will delve deeper into the question of what an exception actually is and how it differs from an error.

Exception or Error?

As you saw last month, the Exception class is not at the top of the anomaly inheritance tree. In fact, both errors and exceptions inherit from a class called Throwable, as seen in Diagram 1.

Diagram 1: The Throwable inheritance tree.

In last month's article, I defined a basic rule-of-thumb for distinguishing between an Error and Exception: An error tends to be an anomaly that is so serious that the application can't continue. This may be something such a hardware failure or a memory issue that corrupts the execution of an application.

The definition from the Java API specifications describes the Throwable, Error, and Exception classes as follows:

The Throwable class:

The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause.


The Error class:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, although a "normal" condition, is also a subclass of Error because most applications should not try to catch it.


The Exception class:

The Exception class and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.


To illustrate, take a look at a simple program that purposely generates an anomaly.

public class Anomaly {

   public static void main(String args[]){

      int a = 0, b = 1, c = 0;

      c = a/0;




Listing 1: Generating an anomaly.

Page 1 of 6

This article was originally published on September 6, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date