

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Control (Chapter 8 )
What facilities for control flow are provided in hardware? unconditonal and conditional branches
(a) What is spaghetti code? Code that makes use of lots gotos that cause the control flow to jump around in unpredictable ways without an overall pattern. (b) Why is spaghetti code undesirable? Spaghetti code is often difficult to maintain, since it is impossible to reason about small pieces of code in isolation, because you never know what code will be the destination of a jump and from where the jump will come.
Rewrite the following structured C program using if’s goto’s and labels instead of while and blocks (i.e., {}): int i = 0; while ( i < 10) { i ++; }
L 0 : i n t i = 0 ; L 1 : i f ( i >= 1 0 ) goto L 4 : L 2 : i ++; L 3 : goto L 1 : L 4 :...
(a) What are exceptions? A programmng language construct for immediately the exiting a nested group of currently active blocks, control structures, and/or functions and continue at a dynamically selected exception handler. (b) What is the original purpose for which exceptions were invented? ‘Exceptional’ situations (i.e., handling errors and other unusual circum- stances) (c) What is an alternative use of exceptions? As an optimization: avoiding unnecessary computation when the result is known earlier than it would be through the ‘normal’ path.
List the three effects of raising an exception.
(a) jump out of a block or function (b) pass data as part of the jump (c) return to a program point that was set up to continue the computation
(^6) α. List the two kinds of constructs provided by all exception mechanisms.
( 1 ) a construct for raising exceptions (e.g., throw in Java), and ( 2 ) a construct for designating code to recover from the exception (e.g., catch in Java).
(^6) β. (a) Do exceptions have dynamic or static scoping?
dynamic (b) Give and explain an example that illustrates the type of scoping of exceptions in ML, Java, or C++. e x c e p t i o n E ; val f = ( ( f n x => 3 + x + ( r a i s e E ) ) handle E => ( f n y => y + 2 ) ) ; val z = ( f ( 2 ) handle E => 4 2 ) ;
This example has two exception handlers for the exception E. One surrounds the definition of an anonymous function (to which the name f is later bound), and the other surrounds the call to f( 2 ). When f( 2 ) is called in the defintion of z, an exception occurs. If exceptions were statically scoped, the exception would be handled by the handler in the definition of f, which lexically surrounds the ‘raise’. But, in fact, exceptions are dynamically scoped, so that handler would only get used if an exception were to occur during the definition of f (which will never happen). Instead, dynamic scoping binds the exception handler that surrounds the call f( 2 ). (c) Why is it desirable for exceptions to have this type of scoping? This allows the flexibility for different callers to hook in different exception handlers and customize the error handling behavior.
(a) What is the type of f? int->int (b) What is the type of f( 10 )? int (c) What would be the type of raise Signal(0) in isolation? 'a (d) What is the type of raise Signal(0) in the context of the definition of f? int (e) What is the type of x+ 8? int