Control I Expressions and Statements - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Control I Expressions and Statements

Description:

handle any remaining uncaught exceptions. CS4303 Hong Lin. 26. Exception handlers (in Ada syntax) ... handle any remaining uncaught exceptions. end; CS4303 Hong ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 30
Provided by: hpcus525
Category:

less

Transcript and Presenter's Notes

Title: Control I Expressions and Statements


1
Chapter 7
  • Control I - Expressions and Statements
  • Part 2

2
Loops and variations on while
  • Guarded Do
  • do b1 -gt s1
  • b2 -gt s2
  • ....
  • bn -gt sn
  • od
  • This statement is repeated until all the b's are
    false
  • (nondeterministically one true b is selected at
    each step)

3
Example
  • What are the possible results?
  • Assume x 1, y 2, z 3
  • do xy -gt y--
  • yz -gt z--
  • zx -gt x--
  • od
  • What if x 1, y 2, z 2?
  • What if x 1, y 1, z 1?

4
Standard form of loop construct
  • pretesting
  • while b do s od
  • Algol68
  • while b DO s END module-2
  • posttesting
  • repeat s until b
  • pascal
  • Syntactic sugar
  • s
  • while not b do s

5
loop-exit in modula-2
  • Sometimes it is helpful to be able to exit a loop
    from arbitrary points inside it.
  • loop-exit in modula-2
  • loop
  • ...
  • if b1 then exit end
  • ...
  • if b2 then exit end
  • ...
  • end

6
break and continue
  • break - exit the loop completely
  • continue - skips the remainder of the body of the
    loop, resuming execution with the next evaluation
    of the control expression
  • an example of the use of break
  • while (1) / always secceeds /
  • if () break

7
For-Loop
  • a common special case of a looping construct
  • C/C/Java
  • for ( e1 e2 e3 ) S
  • Modula-2
  • FOR I 0 to MAX STEP 2 DO
  • END
  • Fortran
  • DO 20 I 0, MAX
  • ...
  • 20 CONTINUE
  • I is the control variable, tested and incremented
    after each iteration

8
Use of Index Variable
  • To gain efficiency in processing for-loop,
    restrictions must be placed on I
  • I can not be changed within the body of loop
  • I is undefined after the loop terminates
  • I must be of restricted types e.g., local
    variables

9
Bound test
  • Modern languages do pretesting
  • Some older Fortran do post testing

10
GOTO controversy
  • It should stay (Fortran)
  • It should be abolished (using loop-exit instead)
  • It should remain, but with severe restrictions on
    its use (Pascal)

11
Exception Handling
  • The control of error conditions or other unusual
    events during the execution of a program.
  • An exception is any unexpected or infrequent
    event.
  • When an exception occurs, it is said to be raised
    or thrown.

12
Exceptions
  • Runtime errors out-of-range array subscripts
    division by zero
  • Static errors (such as syntax and type errors) in
    interpreted languages
  • Any unusual or infrequent event end of file end
    of page bad input timeout

13
Exception Handler
  • a procedure or code sequence that is designed to
    be executed when a particular exception is raised
    and that is supposed to make it possible for
    normal execution to resume
  • An exception handler is said to handle or catch
    an exception

14
history
  • Pioneered by PL/I in the 1960s
  • significantly advanced by CLU in the 1970s
  • design questions were largely resolved in the
    1980s and early 1990s
  • virtually all major current languages have
    built-in exception handling mechanisms, e.g.,
    C, Java, Ada, ML, and Common Lisp

15
Robustness
  • a program must be able to recover from errors and
    continue execution
  • exception handling is an attempt to imitate in a
    programming language the features of a hardware
    interrupt or error trap
  • the program is said to be aborted, or crashes,
    if the underlying machine or operating system
    takes control

16
Asynchronous exceptions
  • Asynchronous exceptions failures occur at a low
    level that lead to situations in which the
    underlying operating system to take drastic
    action to terminate a program
  • e.g., failure of a hardware device, memory
    allocation problems, communication problems
  • can occur at any moment
  • not in response to the execution of program code
  • the program cannot do anything about it

17
Synchronous exceptions
  • Exceptions that occur in direct response to
    actions by the program
  • e.g., open a file, perform a particular
    calculation
  • user-defined exceptions can only be synchronous
  • predefined or library exceptions may include a
    number of asynchronous exceptions

18
Deal with exceptions in a language
  • Find out exception conditions before error occurs
  • if (y 0)
  • handleError(denominator in ratio is zero)
  • else
  • ratio x / y

19
If error occurs
  • Pass the error condition back to the caller
  • e.g., in C
  • enum ErrorKind OutOfInput, BadChar, Normal
  • ErrorKind getNumber ( unsigned result )
  • int ch fgetc(input)
  • if (ch EOF) return OutOfInput
  • else if (! Isdigit(ch)) return BadChar
  • / continue to compute /
  • result
  • return Normal

20
Pass an exception handling procedure into the
procedure as a parameter (1)
  • enum ErrorKind OutOfInput, BadChar, Normal
  • typedef void (ErrorProc)(ErrorKind)
  • ...
  • unsigned value
  • ...
  • void handler (ErrorKind error)
  • ...
  • ...

21
Pass an exception handling procedure into the
procedure as a parameter (2)
  • unsigned getNumber ( ErrorProc handle )
  • unsigned result
  • int ch fgetc(input)
  • if (ch EOF) handle(OutOfInput)
  • else if (! Isdigit(ch)) handle(BadChar)
  • / continue to compute /
  • result
  • return result
  • ...
  • value getNumber (handler)

22
Declaring exceptions in advance
  • Declaring exceptions in advance of their
    occurrence and specifying what a program is to do
    if an exception occurs
  • Factors to consider
  • Exceptions predefined exceptions user defined
    exception scopes
  • Exception handlers default handlers provided for
    predefined exceptions user defined exception
    handlers scopes
  • Control control pass to a handler control pass
    after a handler is executed

23
Exception handling mechanisms used by
block-structured languages
  • Adas predefined exceptions
  • CONSTRAINT_ERROR subrange constraints array
    index constraints
  • NUMERIC_ERROR overflow division by zero
  • PROGRAM_ERROR errors occur during the dynamic
    processing of a declaration
  • STORAGE_ERROR failure of dynamic memory
    allocation
  • TASKING_ERROR errors occur during concurrency
    control

24
User defined exceptions
  • Syntax (in Ada or ML)
  • exception Trouble
  • exception Big_Trouble
  • The declaration of an exception follows the same
    static scope rules as other declaration

25
Exception handlers
  • try-catch blocks in C
  • try
  • // normal code goes here
  • catch (Trouble t)
  • // handle trouble, if possible
  • displayMessage(trouble here!)
  • catch (Big_Trouble b)
  • // handle big trouble, if possible
  • catch ()
  • // actual three dots here, not an ellipsis
  • // handle any remaining uncaught exceptions

26
Exception handlers (in Ada syntax)
  • begin
  • -- normal code goes here
  • exception
  • when Trouble gt
  • -- handle trouble, if possible
  • displayMessage(trouble here!)
  • ...
  • When Big_Trouble gt
  • -- handle big trouble, if possible
  • ...
  • when others gt
  • -- handle any remaining uncaught exceptions
  • end

27
Continuation of the exception
  • The environment under which execution continues
    after an exception
  • Resumption model resume the execution at the
    point where the exception occurred
  • Termination model cause the block in which the
    exception occurred to terminate execution

28
Dynamic propagation of exceptions
  • Ada (also C, ML, ...) follows termination model
    for exceptions
  • Dynamic propagation of exceptions If the block
    has no exception part, or if no handler for the
    exception exists, the block is exited, and the
    exception is re-raised in the controlling block
    in the execution path (call unwinding or stack
    unwinding)
  • If an exception is raised for which no handler is
    found in the dynamic chain, a PROGRAM_ERROR is
    raised by the system, and execution is aborted

29
End of Chapter 7 Part 2
Write a Comment
User Comments (0)
About PowerShow.com