Review Questions
1. Define exception, exception handler, raising an exception,
disabling an exception, continuation, finalization, and built-in
exception.
An exception is an unusual event that is detectable by either
hardware or software and that may require special processing. The
special processing that may be required when an exception is detected is
called exception handling. The processing is done by a code unit
or segment called an exception handler. An exception is raised when its
associated event occurs. In some situations, it may be desirable to
ignore certain hardware-detectable exceptions—for example, division by
zero—for a time. This action would be done by disabling the exception.
After an exception handler executes, either control can transfer to
somewhere in the program outside of the handler code or
Program
execution can simply terminate. We term this the question of control
continuation after handler execution, or simply continuation. In some
situations, it is necessary to complete some computation regardless of
how subprogram
execution terminates. The ability to specify such a computation is
called finalization. Built-in exceptions have a built-in meaning, it is
generally inadvisable to use these to signal program-specific error
conditions. Instead we introduce a new exception using an exception
declaration, and signal it using a raise expression when a run-time
violation occurs. That way we can associate specific exceptions with
specific pieces of code, easing the process of tracking down the source
of the error.
7. Where are unhandled exceptions propagated in Ada if raised in a subprogram?
A block? A package body? A task?
When an exception is raised in a block, in either its declarations or
executable statements, and the block has no handler for it, the
exception is propagated to the next larger enclosing static scope,
which is the code that “called” it. The point to which the exception is
propagated is just after the end of the block in which it occurred,
which is its “return” point. When an exception is raised in a package
body and the package body has no handler for the exception, the
exception is propagated to the declaration section of the unit
containing the package declaration. If the package happens to be a
library unit (which is separately compiled), the program is terminated.
If an exception occurs at the outermost level in a task body (not in a
nested block) and the task contains a handler for the exception, that
handler is executed and the task is marked as being completed. If the
task does not have a handler for the exception, the task is simply
marked as being completed; the exception is not propagated. The control
mechanism of a task is too complex to lend itself to a reasonable and
simple answer to the question of where its unhandled exceptions should
be propagated.
10. What are the four exceptions defined in the Standard package of Ada?
The four exception defined in the standard package of Ada are Constraint_Error, Program_Error, Storage_Error, Tasking_Error
11. What is the use of suppress pragma in Ada?
An Ada pragma is a directive to the compiler. Certain run-time checks
that are parts of the built-in exceptions can be disabled in Ada
programs by use of the Suppress pragma, the simple form of which is
pragma Suppress(check_name)
where check_name is the name of a particular exception check. The
Suppress pragma can appear only in declaration sections. When it
appears, the specified check may be suspended in the associated block or
program unit of which the declaration section is a part. Explicit
raises are not affected by Suppress. Although it is not required, most
Ada compilers implement the Suppress pragma.
13. Describe three problems with Ada’s exception handling.
There are several problems with Ada’s exception handling. One problem
is the propagation model, which allows exceptions to be propagated to
an outer scope in which the exception is not visible. Also, it is not
always possible to determine the origin of propagated exceptions.
Another problem is the inadequacy of exception handling for tasks. For
example, a task that raises an exception but does not handle it simply
dies. Finally, when support for object-oriented programming was added in
Ada 95,
its exception handling was not extended to deal with the new
constructs. For example, when several objects of a class are created and
used in a block and one of them propagates an exception, it is
impossible to determine which one raised the exception.
14. What is the name of all C++ exception handlers?
Each catch function is an exception handler. A catch function can have only a single formal parameter,
which is similar to a formal parameter in a function definition in C++,
including the possibility of it being an ellipsis (. . .). A handler
with an ellipsis formal parameter is the catch-all handler; it is
enacted for any raised exception if no appropriate handler was found.
The formal parameter also can be a naked type specifier, such as float,
as in a function prototype. In such a case, the only purpose of the
formal parameter is to make the handler uniquely identifiable. When
information about the exception is to be passed to the handler, the
formal parameter includes a variable name that is used for that purpose.
Because the class of the parameter can be any user-defined class, the
parameter can include as many data members as are
necessary.
15. Which standard libraries define and throw the exception out_of_range in C++?
The exception out_of_range in C++ thrown by library container classes
16. Which standard libraries define and throw the exception overflow_error in C++?
the exception overflow_error in C++ thrown by math library functions
19. State the similarity between the exception handling mechanism in C++ and Ada
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.
20. State the differences between the exception handling mechanism in C++ and Ada
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.
24. What is the difference between checked and unchecked exceptions in Java?
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.
26. How can an exception handler be written in Java so that it handles any exception?
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.
28. What is the purpose of Java finally clause?
A finally clause is placed at the end of the list of handlers just
after a complete try construct. 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
Problem Set
1 . What mechanism did early programming languages provide to detect or attempt to deal with errors?
Early programming languages were designed and implemented in such a way that the user program
could neither detect nor attempt to deal with such errors. In these
languages, the occurrence of such an error simply causes the program to
be terminated and control to be transferred to the operating system. The
typical operating system reaction to a run-time error is to display a
diagnostic message, which may be meaningful and therefore useful, or
highly cryptic. After displaying the message, the program is terminated.
2. Describe the approach for the detection of subscript range errors used in C and Java.
Java compilers usually generate code to check the correctness of
every subscript expression (they do not generate such code when it can
be determined at compile time that a subscript expression cannot have an
out-of-range value, for example, if the subscript is a literal).
In C, subscript ranges are not checked because the cost of such
checking was (and still is) not believed to be worth the benefit of
detecting such errors. In some compilers for some languages, subscript
range checking can be selected (if not turned on by default) or turned
off (if it is on by default) as desired in the program or in the command
that executes the compiler.
5. From a textbook on FORTRAN, determine how exception handling is done in FORTRAN programs.
For example, a Fortran “Read” statement can intercept inputerrors and
end-of-file conditions, both of which are detected by the input device
hardware. In both cases, the Read statement can specify the label of
some statement in the user program that deals with the condition. In the
case of the end-of-file, it is clear that the condition is not always
considered an error. In most cases, it is nothing more than a signal
that one kind of processing is completed and another kind must begin. In
spite of the obvious difference between end-of-file and events that are
always errors, such as a failed input process, Fortran handles both
situations with the same mechanism.
6. In languages without exception-handling facilities, it is
common to have most subprograms include an “error” parameter, which can
be set to some value representing “OK” or some other value representing
“error in procedure”. What advantage does a linguistic
exception-handling facility like that of Ada have over this method?
There are several advantages of a linguistic mechanism for handling
exceptions, such as that found in Ada, over simply using a flag error
parameter in all subprograms. One advantage is that the code to test the
flag after every call is eliminated. Such testing makes programs longer
and harder to read. Another advantage is that exceptions can be
propagated farther than one level of control in a uniform and implicit
way. Finally, there is the advantage that all programs use a uniform
method for dealing with unusual circumstances, leading to enhanced
readability.
7. In a language without exception handling facilities, we could
send an error-handling procedure as a parameter to each procedure that
can detect errors that must be handled. What disadvantages are there to
this method?
There are several disadvantages of sending error handling subprograms
to other subprograms. One is that it may be necessary to send several
error handlers to some subprograms, greatly complicating both the
writing and execution of calls. Another is that there is no method of
propagating exceptions, meaning that they must all be handled locally.
This complicates exception handling, because it requires more attention
to handling in more places.
Tidak ada komentar:
Posting Komentar