
- •New to the Tenth Edition
- •Preface
- •Acknowledgments
- •About the Author
- •Contents
- •1.1 Reasons for Studying Concepts of Programming Languages
- •1.2 Programming Domains
- •1.3 Language Evaluation Criteria
- •1.4 Influences on Language Design
- •1.5 Language Categories
- •1.6 Language Design Trade-Offs
- •1.7 Implementation Methods
- •1.8 Programming Environments
- •Summary
- •Problem Set
- •2.1 Zuse’s Plankalkül
- •2.2 Pseudocodes
- •2.3 The IBM 704 and Fortran
- •2.4 Functional Programming: LISP
- •2.5 The First Step Toward Sophistication: ALGOL 60
- •2.6 Computerizing Business Records: COBOL
- •2.7 The Beginnings of Timesharing: BASIC
- •2.8 Everything for Everybody: PL/I
- •2.9 Two Early Dynamic Languages: APL and SNOBOL
- •2.10 The Beginnings of Data Abstraction: SIMULA 67
- •2.11 Orthogonal Design: ALGOL 68
- •2.12 Some Early Descendants of the ALGOLs
- •2.13 Programming Based on Logic: Prolog
- •2.14 History’s Largest Design Effort: Ada
- •2.15 Object-Oriented Programming: Smalltalk
- •2.16 Combining Imperative and Object-Oriented Features: C++
- •2.17 An Imperative-Based Object-Oriented Language: Java
- •2.18 Scripting Languages
- •2.19 The Flagship .NET Language: C#
- •2.20 Markup/Programming Hybrid Languages
- •Review Questions
- •Problem Set
- •Programming Exercises
- •3.1 Introduction
- •3.2 The General Problem of Describing Syntax
- •3.3 Formal Methods of Describing Syntax
- •3.4 Attribute Grammars
- •3.5 Describing the Meanings of Programs: Dynamic Semantics
- •Bibliographic Notes
- •Problem Set
- •4.1 Introduction
- •4.2 Lexical Analysis
- •4.3 The Parsing Problem
- •4.4 Recursive-Descent Parsing
- •4.5 Bottom-Up Parsing
- •Summary
- •Review Questions
- •Programming Exercises
- •5.1 Introduction
- •5.2 Names
- •5.3 Variables
- •5.4 The Concept of Binding
- •5.5 Scope
- •5.6 Scope and Lifetime
- •5.7 Referencing Environments
- •5.8 Named Constants
- •Review Questions
- •6.1 Introduction
- •6.2 Primitive Data Types
- •6.3 Character String Types
- •6.4 User-Defined Ordinal Types
- •6.5 Array Types
- •6.6 Associative Arrays
- •6.7 Record Types
- •6.8 Tuple Types
- •6.9 List Types
- •6.10 Union Types
- •6.11 Pointer and Reference Types
- •6.12 Type Checking
- •6.13 Strong Typing
- •6.14 Type Equivalence
- •6.15 Theory and Data Types
- •Bibliographic Notes
- •Programming Exercises
- •7.1 Introduction
- •7.2 Arithmetic Expressions
- •7.3 Overloaded Operators
- •7.4 Type Conversions
- •7.5 Relational and Boolean Expressions
- •7.6 Short-Circuit Evaluation
- •7.7 Assignment Statements
- •7.8 Mixed-Mode Assignment
- •Summary
- •Problem Set
- •Programming Exercises
- •8.1 Introduction
- •8.2 Selection Statements
- •8.3 Iterative Statements
- •8.4 Unconditional Branching
- •8.5 Guarded Commands
- •8.6 Conclusions
- •Programming Exercises
- •9.1 Introduction
- •9.2 Fundamentals of Subprograms
- •9.3 Design Issues for Subprograms
- •9.4 Local Referencing Environments
- •9.5 Parameter-Passing Methods
- •9.6 Parameters That Are Subprograms
- •9.7 Calling Subprograms Indirectly
- •9.8 Overloaded Subprograms
- •9.9 Generic Subprograms
- •9.10 Design Issues for Functions
- •9.11 User-Defined Overloaded Operators
- •9.12 Closures
- •9.13 Coroutines
- •Summary
- •Programming Exercises
- •10.1 The General Semantics of Calls and Returns
- •10.2 Implementing “Simple” Subprograms
- •10.3 Implementing Subprograms with Stack-Dynamic Local Variables
- •10.4 Nested Subprograms
- •10.5 Blocks
- •10.6 Implementing Dynamic Scoping
- •Problem Set
- •Programming Exercises
- •11.1 The Concept of Abstraction
- •11.2 Introduction to Data Abstraction
- •11.3 Design Issues for Abstract Data Types
- •11.4 Language Examples
- •11.5 Parameterized Abstract Data Types
- •11.6 Encapsulation Constructs
- •11.7 Naming Encapsulations
- •Summary
- •Review Questions
- •Programming Exercises
- •12.1 Introduction
- •12.2 Object-Oriented Programming
- •12.3 Design Issues for Object-Oriented Languages
- •12.4 Support for Object-Oriented Programming in Smalltalk
- •12.5 Support for Object-Oriented Programming in C++
- •12.6 Support for Object-Oriented Programming in Objective-C
- •12.7 Support for Object-Oriented Programming in Java
- •12.8 Support for Object-Oriented Programming in C#
- •12.9 Support for Object-Oriented Programming in Ada 95
- •12.10 Support for Object-Oriented Programming in Ruby
- •12.11 Implementation of Object-Oriented Constructs
- •Summary
- •Programming Exercises
- •13.1 Introduction
- •13.2 Introduction to Subprogram-Level Concurrency
- •13.3 Semaphores
- •13.4 Monitors
- •13.5 Message Passing
- •13.6 Ada Support for Concurrency
- •13.7 Java Threads
- •13.8 C# Threads
- •13.9 Concurrency in Functional Languages
- •13.10 Statement-Level Concurrency
- •Summary
- •Review Questions
- •Problem Set
- •14.1 Introduction to Exception Handling
- •14.2 Exception Handling in Ada
- •14.3 Exception Handling in C++
- •14.4 Exception Handling in Java
- •14.5 Introduction to Event Handling
- •14.6 Event Handling with Java
- •14.7 Event Handling in C#
- •Review Questions
- •Problem Set
- •15.1 Introduction
- •15.2 Mathematical Functions
- •15.3 Fundamentals of Functional Programming Languages
- •15.4 The First Functional Programming Language: LISP
- •15.5 An Introduction to Scheme
- •15.6 Common LISP
- •15.8 Haskell
- •15.10 Support for Functional Programming in Primarily Imperative Languages
- •15.11 A Comparison of Functional and Imperative Languages
- •Review Questions
- •Problem Set
- •16.1 Introduction
- •16.2 A Brief Introduction to Predicate Calculus
- •16.3 Predicate Calculus and Proving Theorems
- •16.4 An Overview of Logic Programming
- •16.5 The Origins of Prolog
- •16.6 The Basic Elements of Prolog
- •16.7 Deficiencies of Prolog
- •16.8 Applications of Logic Programming
- •Review Questions
- •Programming Exercises
- •Bibliography
- •Index

|
|
|
14.4 Exception Handling in Java |
647 |
catch(NegativeInputException& e) { //**Handler for |
|
|||
|
|
|
//** negative input |
|
cout << "Limits |
Frequency" << endl; |
|
||
for (index = 0; index < 10; index++) { |
|
|||
limit_1 |
= |
10 * index; |
|
|
limit_2 |
= |
limit_1 + 9; |
|
|
if (index |
== 9) |
|
|
|
limit_2 |
= 100; |
|
|
cout << limit_1 << limit_2 << freq[index] << endl;
}//* end of for (index = 0)
}//* end of catch (NegativeInputException& e)
}//* end of main
This program is meant to illustrate the mechanics of C++ exception handling. Note that the index range exception is often handled in C++ by overloading the indexing operation, which could then raise the exception, rather than the direct detection of the indexing operation with the selection construct used in our example.
14.3.6Evaluation
In some ways, the C++ exception-handling mechanism is similar to that of Ada. For example, unhandled exceptions in functions are propagated to the function’s caller. However, in other ways, the C++ design is quite different: There are no predefined hardware-detectable exceptions that can be handled by the user, and exceptions are not named. Exceptions are connected to handlers through a parameter type in which the formal parameter may be omitted. The type of the formal parameter of a handler determines the condition under which it is called but may have nothing whatsoever to do with the nature of the raised exception. Therefore, the use of predefined types for exceptions certainly does not promote readability. It is much better to define classes for exceptions with meaningful names in a meaningful hierarchy that can be used for defining exceptions. The exception parameter provides a way to pass information about an exception to the exception handler.
14.4 Exception Handling in Java
In Chapter 13, the Java example program includes the use of exception handling with little explanation. This section describes the details of Java’s exception-handling capabilities.
Java’s exception handling is based on that of C++, but it is designed to be more in line with the object-oriented language paradigm. Furthermore, Java includes a collection of predefined exceptions that are implicitly raised by the Java Virtual Machine ( JVM).

648 |
Chapter 14 Exception Handling and Event Handling |
14.4.1Classes of Exceptions
All Java exceptions are objects of classes that are descendants of the Throwable class. The Java system includes two predefined exception classes that are subclasses of Throwable: Error and Exception. The Error class and its descendants are related to errors that are thrown by the Java run-time system, such as running out of heap memory. These exceptions are never thrown by user programs, and they should never be handled there. There are two system-defined direct descendants of Exception: RuntimeException and IOException. As its name indicates, IOException is thrown when an error has occurred in an input or output operation, all of which are defined as methods in the various classes defined in the package java.io.
There are predefined classes that are descendants of RuntimeException. In most cases, RuntimeException is thrown (by the JVM4) when a user program causes an error. For example, ArrayIndexOutOfBoundsException, which is defined in java.util, is a commonly thrown exception that descends from RuntimeException. Another commonly thrown exception that descends from RuntimeException is NullPointer Exception.
User programs can define their own exception classes. The convention in Java is that user-defined exceptions are subclasses of Exception.
14.4.2Exception Handlers
The exception handlers of Java have the same form as those of C++, except that every catch must have a parameter and the class of the parameter must be a descendant of the predefined class Throwable.
The syntax of the try construct in Java is exactly as that of C++, except for the finally clause described in Section 14.4.6.
14.4.3Binding Exceptions to Handlers
Throwing an exception is quite simple. An instance of the exception class is given as the operand of the throw statement. For example, suppose we define an exception named MyException as
class MyException extends Exception { public MyException() {}
public MyException(String message) { super (message);
}
}
This exception can be thrown with
4.The Java specification also requires JIT compilers to detect these exceptions and throw
RunTimeException when they occur.

14.4 Exception Handling in Java |
649 |
throw new MyException();
The creation of the instance of the exception for the throw could be done separately from the throw statement, as in
MyException myExceptionObject = new MyException();
...
throw myExceptionObject;
One of the two constructors we have included in our new class has no parameter and the other has a String object parameter that it sends to the superclass (Exception), which displays it. Therefore, our new exception could be thrown with
throw new MyException
("a message to specify the location of the error");
The binding of exceptions to handlers in Java is similar to that of C++. If an exception is thrown in the compound statement of a try construct, it is bound to the first handler (catch function) immediately following the try clause whose parameter is the same class as the thrown object, or an ancestor of it. If a matching handler is found, the throw is bound to it and it is executed.
Exceptions can be handled and then rethrown by including a throw statement without an operand at the end of the handler. The newly thrown exception will not be handled in the same try where it was originally thrown, so looping is not a concern. This rethrowing is usually done when some local action is useful, but further handling by an enclosing try clause or a try clause in the caller is necessary. A throw statement in a handler could also throw some exception other than the one that transferred control to this handler.
To ensure that exceptions that can be thrown in a try clause are always handled in a method, a special handler can be written that matches all exceptions that are derived from Exception simply by defining the handler with an Exception type parameter, as in
catch (Exception genericObject) {
...
}
Because a class name always matches itself or any ancestor class, any class derived from Exception matches Exception. Of course, such an exception handler should always be placed at the end of the list of handlers, for it will block the use of any handler that follows it in the try construct in which it appears. This occurs because the search for a matching handler is sequential, and the search ends when a match is found.

650 |
Chapter 14 Exception Handling and Event Handling |
14.4.4Other Design Choices
During program execution, the Java run-time system stores the class name of every object in the program. The method getClass can be used to get an object that stores the class name, which itself can be gotten with the getName method. So, we can retrieve the name of the class of the actual parameter from the throw statement that caused the handler’s execution. For the handler shown earlier, this is done with
genericObject.getClass().getName()
In addition, the message associated with the parameter object, which is created by the constructor, can be gotten with
genericObject.getMessage()
Furthermore, in the case of user-defined exceptions, the thrown object could include any number of data fields that might be useful in the handler.
The throws clause of Java has the appearance and placement (in a program) that is similar to that of the throw specification of C++. However, the semantics of throws is somewhat different from that of the C++ throw clause.
The appearance of an exception class name in the throws clause of a Java method specifies that that exception class or any of its descendant exception classes can be thrown but not handled by the method. For example, when a method specifies that it can throw IOException, it means it can throw an IOException object or an object of any of its descendant classes, such as EOFException, and it does not handle the exception it throws.
Exceptions of class Error and RuntimeException and their descendants are called unchecked exceptions. All other exceptions are called checked exceptions. Unchecked exceptions are never a concern of the compiler. However, the compiler ensures that all checked exceptions a method can throw are either listed in its throws clause or handled in the method. Note that checking this at compile time differs from C++, in which it is done at run time. The reason why exceptions of the classes Error and RuntimeException and their descendants are unchecked is that any method could throw them. A program can catch unchecked exceptions, but it is not required.
As is the case with C++, a method cannot declare more exceptions in its throws clause than the method it overrides, though it may declare fewer. So if a method has no throws clause, neither can any method that overrides it. A method can throw any exception listed in its throws clause, along with any of its descendant classes.
A method that does not directly throw a particular exception, but calls another method that could throw that exception, must list the exception in its throws clause. This is the reason the buildDist method (in the example in the next subsection), which uses the readLine method, must specify IOException in the throws clause of its header.

14.4 Exception Handling in Java |
651 |
A method that does not include a throws clause cannot propagate any checked exception. Recall that in C++, a function without a throw clause can throw any exception.
A method that calls a method that lists a particular checked exception in its throws clause has three alternatives for dealing with that exception: First, it can catch the exception and handle it. Second, it can catch the exception and throw an exception that is listed in its own throws clause. Third, it could declare the exception in its own throws clause and not handle it, which effectively propagates the exception to an enclosing try clause, if there is one, or to the method’s caller, if there is no enclosing try clause.
There are no default exception handlers, and it is not possible to disable exceptions. Continuation in Java is exactly as in C++.
14.4.5An Example
Following is the Java program with the capabilities of the C++ program in Section 14.3.5:
//Grade Distribution
//Input: A list of integer values that represent
//grades, followed by a negative number
//Output: A distribution of grades, as a percentage for
//each of the categories 0-9, 10-19, ...,
//90-100.
import java.io.*;
// The exception definition to deal with the end of data class NegativeInputException extends Exception {
public NegativeInputException() { System.out.println("End of input data reached");
}//** end of constructor
}//** end of NegativeInputException class
class GradeDist { int newGrade, index,
limit_1, limit_2;
int [] freq = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
void buildDist() throws IOException {
DataInputStream in = new DataInputStream(System.in);
try {
while (true) {
System.out.println("Please input a grade"); newGrade = Integer.parseInt(in.readLine()); if (newGrade < 0)

652 |
Chapter 14 Exception Handling and Event Handling |
throw new NegativeInputException(); index = newGrade / 10;
try { freq[index]++;
}//** end of inner try clause
catch(ArrayIndexOutOfBoundsException e) { if (newGrade == 100)
freq [9]++;
else
System.out.println("Error - new grade: " + newGrade + " is out of range");
}//** end of catch (ArrayIndex...
}//** end of while (true) ...
}//** end of outer try clause
catch(NegativeInputException e) { |
|
||
System.out.println ("\nLimits |
Frequency\n"); |
||
for (index = 0; index < 10; index++) { |
|||
limit_1 |
= |
10 * index; |
|
limit_2 |
= |
limit_1 + 9; |
|
if (index |
== 9) |
|
|
limit_2 |
= 100; |
|
|
System.out.println("" + limit_1 + " - " + |
|||
|
|
limit_2 + " |
" + freq [index]); |
}//** end of for (index = 0; ...
}//** end of catch (NegativeInputException ...
}//** end of method buildDist
The exception for a negative input, NegativeInputException, is defined in the program. Its constructor displays a message when an object of the class is created. Its handler produces the output of the method. ArrayIndexOutOfBoundsException is a predefined unchecked exception that is thrown by the Java run-time system. In both of these cases, the handler does not include an object name in its parameter. In neither case would a name serve any purpose. Although all handlers get objects as parameters, they often are not useful.
14.4.6The finally Clause
There are some situations in which a process must be executed regardless of whether a try clause throws an exception and regardless of whether a thrown exception is caught in a method. One example of such a situation is a file that must be closed. Another is if the method has some external resource that must be freed in the method regardless of how the execution of the method terminates. The finally clause was designed for these kinds of needs. A finally clause is placed at the end of the list of handlers just after a complete try construct. In general, the try construct and its finally clause appear as

14.4 Exception Handling in Java |
653 |
try {
...
}
catch (...) {
...
}
... //** More handlers finally {
...
}
The semantics of this construct is as follows: If the try clause throws no exceptions, the finally clause is executed before execution continues after the try construct. If the try clause throws an exception and it is caught by a following handler, the finally clause is executed after the handler completes its execution. If the try clause throws an exception but it is not caught by a handler following the try construct, the finally clause is executed before the exception is propagated.
A try construct with no exception handlers can be followed by a finally clause. This makes sense, of course, only if the compound statement has a throw, break, continue, or return statement. Its purpose in these cases is the same as when it is used with exception handling. For example, consider the following:
try {
for (index = 0; index < 100; index++) {
...
if (... ) { return;
} //** end of if
...
}//** end of for
}//** end of try clause
finally {
...
}//** end of try construct
The finally clause here will be executed, regardless of whether the return terminates the loop or it ends normally.
14.4.7Assertions
In the discussion of Plankalkül in Chapter 2, we mentioned that it included assertions. Assertions were added to Java in version 1.4. To use them, it is necessary to enable them by running the program with the enableassertions (or ea) flag, as in

654 |
Chapter 14 Exception Handling and Event Handling |
java -enableassertions MyProgram
There are two possible forms of the assert statement:
assert condition;
assert condition : expression;
In the first case, the condition is tested when execution reaches the assert. If the condition evaluates to true, nothing happens. If it evaluates to false, the AssertionError exception is thrown. In the second case, the action is the same, except that the value of the expression is passed to the AssertionError constructor as a string and becomes debugging output.
The assert statement is used for defensive programming. A program may be written with many assert statements, which ensure that the program’s computation is on track to produce correct results. Many programmers put in such checks when they write a program, as an aid to debugging, even though the language they are using does not support assertions. When the program is sufficiently tested, these checks are removed. The advantage of assert statements, which have the same purpose, is that they can be disabled without removing them from the program. This saves the effort of removing them and also allows their use during subsequent program maintenance.
14.4.8Evaluation
The Java mechanisms for exception handling are an improvement over the C++ version on which they are based.
First, a C++ program can throw any type defined in the program or by the system. In Java, only objects that are instances of Throwable or some class that descends from it can be thrown. This separates the objects that can be thrown from all of the other objects (and nonobjects) that inhabit a program. What significance can be attached to an exception that causes an int value to be thrown?
Second, a C++ program unit that does not include a throw clause can throw any exception, which tells the reader nothing. A Java method that does not include a throws clause cannot throw any checked exception that it does not handle. Therefore, the reader of a Java method knows from its header what exceptions it could throw but does not handle. A C++ compiler ignores throw clauses, but a Java compiler ensures that all exceptions that a method can throw are listed in its throws clause.
Third, the addition of the finally clause is a great convenience in certain situations. It allows cleanup kinds of actions to take place regardless of how a compound statement terminated.
Finally, the Java run-time system implicitly throws a variety of predefined exceptions, such as for array indices out of range and dereferencing null references, which can be handled by any user program. A C++ program can handle only those exceptions that it explicitly throws (or that are thrown by library classes it uses).