Friday, June 28, 2013

Concepts of Programming Languages Chapter 14


Concepts of Programming Languages Chapter 14


Name            : Erland
NIM              : 1601218035
Lecturer       : Tri Djoko Wahjono, Ir., M.Sc. (D0206)
Assignment  : Concept of programming languages Chapter 14

Review Question

       1. Define exception, exception handler, raising an exception, disabling 
           an exception, continuation, finalization, and built-in exception.
           Answer:
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.

       2. When is an exception thrown or raised ?
            Answer:
- When an event associated with an exception occurs

       3. What are the advantages of having support for exception handling 
            built in to a language ?
            Answer:


- Without built-in exception handling, the code to detect error condition can be a clutter to the program. Existence of built-in exception handling would simplify a source program.

        4. Give an example of hardware-detectable execution.
             Answer:
- Division by zero

        5. What does it mean for an exception to be bound to an exception handler ?
            Answer:
- A specific exception is to be handled by a specific exception handler also, because different exceptions are to be treated differently.

         6. What is exception propagation in Ada ?
              Answer:
- A powerful tool for constructing more reliable software systems.

         7. Where are unhandled exceptions propagated in Ada if raised in a 
             subprogram?A block? A package body? A task?
             Answer:
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.

       8. Where does execution continue after an exception is handled in Ada ?
            Answer:
- Control simply continues after the exception clause.

        10. What are the four exceptions defined in the Standard package of Ada ?
              Answer:
- Constraint_Error,Program_Error,Storage_Error, and Tasking_Error.

         11. Are there any predefined exceptions in Ada ?
               Answer:
- Yes, there are, for example Ada.Text_IO defines the End_Error exception.

          12. What is the use of Suppress pragma in Ada ?
                Answer:
- Run-time checks that are parts of built-in exceptions can be disabled in Ada programs

Problem Set

           1. What mechanism did early programming languages provide to 
                detect to attempt to deal with errors ?


- There were no possibility for the user program to detect or attempt to deal with errors. In case if error happens, the program will be terminated and control will be transferred to the operating system.

          2. Describe the approach for the detection of subscript range errors used 
               in C and Java.


- C does not check subscript ranges. While in Java, compilers usually generate a code to check the correctness of every subscript expression. If any exception generates, an unchecked exception is thrown.



          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.