6. Q: 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?
A: 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. Q: 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?
A: 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.
8. Q: Compare the methods suggested in Problems 6 and 7. Which do you
think is better and why?
A: When an exception occurs, the normal flow of execution is abandoned and the exception is handed up the call sequence until a matching handler is found. Any declarative region (except a package specification) can have a handler. The handler names the exceptions it will handle. By moving up the call sequence, exceptions can become anonymous; in this case, they can only be handled be the other handler.
9. Q: Write a comparative analysis of the throw clause of C++ and the
throws clause of Java.
A: Throw statement is used to retrieve the name of the class with the actual parameter, while the throws clause of Java specifies that exception class or any of its descendant exception classes can be thrown but no handled by the method.
10. Q: Compare the exception-handling facilities of C++ with those of Ada.
Which design, in your opinion, is the most flexible? Which makes it possible
to write more reliable programs?
A: Features introduced in C++ include declarations as statements, function-like casts, new/delete, bool, reference types, const,
inline functions, default arguments, function overloading,
namespaces, classes (including all class-related features such as inheritance, member functions, virtual functions, abstract classes, and constructors), operator overloading, templates, the:: operator, exception handling, run-time type identification, and more type checking in several cases. Comments starting with two slashes ("//") were originally part of
BCPL, and were reintroduced in C++. Several features of C++ were later adopted by C, including const,
inline, declarations in
for loops, and C++-style comments (using the // symbol).