Richard G Baldwin (512) 223-4758, baldwin@austin.cc.tx.us, http://www2.austin.cc.tx.us/baldwin/

Brief Introduction to Exceptions

Java Programming, Lecture Notes # 30, Revised 10/03/99.

Preface
Discussion
Review

Preface

Students in Prof. Baldwin's Introductory Java Programming classes at ACC are responsible for knowing and understanding all of the material in this lesson (except that they are not responsible for detailed information that is specific to C++).

The detailed material on C++ is provided as supplementary material for the benefit of those persons who are already familiar with C++ and who are making the transition into Java.

Discussion

What is an exception?

Campione and Walrath, The Java Tutorial, http://java.sun.com/books/Series/Tutorial/TOC.html, provide an excellent definition of an exception:

"An exception is an event that occurs during the execution of a program that prevents the continuation of the normal flow of instructions."

A very common example of an exception given in textbooks is code that attempts to divide by zero (this is easy to demonstrate).

Throwing an exception

Common terminology states that when this happens, the system throws an exception. If a thrown exception is not caught, a runtime error may occur.

Purpose of exception handling

The purpose of exception handling is to make it possible for the program to either attempt to recover from the problem, or at worst shut down the program in a graceful manner, whenever an exception occurs. This is a complex topic that will be discussed in much more detail in a subsequent lesson.

Java and C++ support exception handling

Briefly, both Java and C++ provide the capability to detect and handle exceptions. In both cases, the exception can be thrown either by the system or by code created by the programmer. However, Java is significantly stronger in the area of exceptions that are thrown automatically. There is a fairly long list of exceptions that will be thrown automatically by the Java system.

As of this writing, this author is aware of only one automatic exception that is built into the "evolving standard" C++ language. With compilers that are striving to comply with the evolving C++ standard, an exception is thrown automatically when an attempt to allocate dynamic memory from the heap fails. (Various proprietary class libraries make provisions for throwing exceptions in special cases, but they tend to be vendor specific. Perhaps the full implementation of the standard template library in C++ will remedy this situation.)

Even when an exception is thrown automatically in C++, the program can ignore it.

Checked exceptions cannot be ignored

Java provides a long list of exceptions that can be thrown by the system known as "checked" exceptions. They cannot be ignored. A function must either specify or catch all "checked" exceptions that can be thrown in order for the program to compile.

An example of specifying an exception

We will discuss the difference between specifying and catching an exception in a subsequent lesson. For now, suffice it to say that the bold portion of the following statement from the Java program specifies or declares an exception that can be thrown by the code inside the main method.

If this specification is not made, the program will not compile.

/*File simple1.java Copyright 1997, R.G.Baldwin
**********************************************************/
class simple1 { //define the controlling class
  public static void main(String[] args) //define main
            throws java.io.IOException {
    int ch1, ch2 = '0';

    System.out.println(
      "Enter some text, terminate with #");

    //Get and save individual bytes
    while( (ch1 = System.in.read() ) != '#') ch2 = ch1;

    //Display the character immediately before the #
    System.out.println("The char before the # was "
      + (char)ch2);
  }//end main
}//End simple1 class.

The program in the above example does not throw any exceptions directly. However, it can throw exceptions indirectly through its call to System.in.read.

Very brief treatment

Again, this is a very brief treatment of a fairly complex topic that will be discussed in much more detail in a subsequent lesson. The topic was included at this early stage for introductory purposes only.

Review

Q - What is the definition of an exception provided by Campione and Walrath in The Java Tutorial?

A - Campione and Walrath, The Java Tutorial, provide the following definition of an exception: "An exception is an event that occurs during the execution of a program that prevents the continuation of the normal flow of instructions."

-end-