Exception Handling and Event Handling
Assignment from Tri Djoko Wahjono
1. Define exception, exception handler, raising an exception, disabling an exception, continuation, finalization, and built-in exception.
– Exception : any unusual event, erroneous or not, that is detectable by either hardware or software and that may require special processing.
– Exception handler : a code unit which processes an exception.
– Raising an exception : When a event associated with an exception occurs
– Disabling an exception : Ignoring a certain hardware-detectable exceptions.
– Continuation : Control transfer to somewhere in the program outside of the handler code or program might terminate .
– Finalization : ability to complete some computation regardless of how subprogram execution terminates.
– Built-in exception : Exception that is not made by the user, but rather comes as default.
3. What are the advantages of having support for exception handling built in to a language?
Error detection code is tedious to write and it clutters the program. Exception handling encourages programmers to consider many different possible errors. Exception propagation allows a high level of reuse of exception handling code
4. What are the design issues for exception handling?
How are user-defined exceptions specified?
Should there be default exception handlers for programs that do not provide their own?
Can built-in exceptions be explicitly raised?
Are hardware-detectable errors treated as exceptions that can be handled?
Are there any built-in exceptions?
How can exceptions be disabled, if at all?
How and where are exception handlers specified and what is their scope?
How is an exception occurrence bound to an exception handler?
Can information about the exception be passed to the handler?
Where does execution continue, if at all, after an exception handler completes its execution? (continuation vs. resumption)
Is some form of finalization provided?
5. What does it mean for an exception to be bound to an exception handler?
A specific exception is to be handled by a specific exception handler also, because different exceptions are to be treated differently.
6. What are the possible frames for exceptions in Ada?
The frame of an exception handler in Ada is either a subprogram body, a package body, a task, or a block
7. Where are unhandled exceptions propagated in Ada if raised in a subprogram? A block? A package body? A task?
If the block or unit in which an exception is raised does not have a handler for that exception, the exception is propagated elsewhere to be handled
Procedures – propagate it to the caller
Blocks – propagate it to the scope in which it appears
Package body – propagate it to the declaration part of the unit that declared the package (if it is a library unit, the program is terminated)
Task – no propagation; if it has a handler, execute it; in either case, mark it “completed”
8. Where does execution continue after an exception is handled in Ada?
Control simply continues after the exception clause.
10. What are the four exceptions defined in the Standard package of Ada?
Constraint_Error, Program_Error, Storage_Error, and Tasking_Error.
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.