Java Reference
In-Depth Information
C
error by requiring that in each CaseItem, stmts . terminatesNormally be false.
Such an analysis is also useful in producing helpful warning messages, since
forgetting a break at the end of a case is a common error.
Other languages include a case statement that is similar in structure to the
switch statement. The latest versions of Java and C
allow enumerations as
well as integers in case statements.
Ada generalizes a case label to a range of case values (e.g., in Java notation,
case 1..10, denotes 10 distinct case values). Semantic checks that look for
duplicate case values and check for complete coverage of possible control
values must be generalized to handle ranges rather than single values.
9.1.7 Exception Handling
Most modern programming languages, including Java and C
,providean ex-
ceptionhandling mechanism. During execution, an exceptionmay be thrown,
either explicitly (via a throw statement) or implicitly (due to an execution er-
ror). Thrown exceptions may be caught by an exception handler.
Exceptions form a clean and general mechanism for identifying and han-
dling unexpected or erroneous situations. They are clearer and more e
cient
than using error flags or gotos. Though we will focus on Java and C
's excep-
tion handling mechanism, most recent language designs, including C
++
,Ada,
and ML, include a very similar exception mechanism.
Java exceptions are typed . An exception throws an object that is an in-
stance of class Throwable or one of its subclasses. The object thrown may
contain fields that characterize the precise nature of the problem the exception
represents, or the class may be empty (with its type signifying all necessary
information).
Java exceptions are classified as either checked or unchecked. A checked
exception thrown in a statement must be caught in an enclosing try statement
or listed in the throws list of the enclosing method or constructor.
An unchecked exception (defined as an object assignable to either class
RuntimeException or class Error) may optionally be handled in a try state-
ment. If uncaught, unchecked exceptionswill terminate execution. Unchecked
exceptions represent errors that may appear almost anywhere (such as access-
ing a null reference or using an illegal array index). These exceptions usually
force termination, so explicit handlers may clutter a program without adding
any benefit (termination is the default for uncaught exceptions).
We will first consider the semantic checking needed for a try statement.
The AST for a try is shown in Figure 9.24. The AST for the catch clauses found
in a try statement is shown in Figure 9.25.
Semantic processing for a try is defined in Figure 9.26 at Marker 29 .All
three components of the try, the try body, the catch clauses, and the optional
 
 
Search WWH ::




Custom Search