Assignments - PowerPoint PPT Presentation

About This Presentation
Title:

Assignments

Description:

The try clause specifies the normal execution. If execution of the try clause raises an exception, then ... Each catch clause can handle one type of exception. ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 9
Provided by: josephn6
Learn more at: https://www.cise.ufl.edu
Category:

less

Transcript and Presenter's Notes

Title: Assignments


1
Assignments
  • A sample main program for Project 2 phase 2 is
    contained inhttp//www.cise.ufl.edu/jnw/COP5555
    /Assignments/Programs/phase2main.c
  • It reads an input program with a filename of form
    file.g, places executable code in a file named
    file.c, then execs the g compiler to create a
    file named file, that is the executable version
    of file.c, which is equivalent in meaning to
    file.g.
  • My sample program does not put translated garnet
    code in the program, but it does create a valid
    C program (hello world).
  • Homework Due Tuesday 6.4, 6.8, 6.29, 6.48

2
Dijkistras Guarded do
  • do B1 -gt S1 B2 -gt S2 ... Bn -gt Snod
  • This behaves like a guarded if that is repeatedly
    executed until all guards are false.
  • The typical while (B) S is equivalent todo B -gt
    S od

3
Variations on While
  • Termination test at end rather than beginning of
    control structuredo S while (B)
  • Mid-loop termination (break) or repitition
    (continue)
  • for-loop of Cfor (initialization go-ahead
    update) S
  • foreach x sequence S
  • CLU Iterator (limited coroutine concept employing
    yield construct to swap context ).

4
Goto Considered Harmful
  • Dijkstras letter http//www.acm.org/classics/oct9
    5/ (dubbed Goto Statement Considered Harmful) put
    forth the proposition that goto statements make
    it difficult to determine how to chart the
    progress of a process.
  • To help programmers avoid the use of goto, many
    languages include labelled control structures and
    allow breaks to exit multiple control constructs.

5
Exceptions
  • An exception is said to be raised when erroneous
    computations are requested or detected.
  • Exceptions cause control to transfer without any
    explicit representation upon the part of the
    programmer.
  • Most programming languages cause a program to
    terminate if an exception is raised unless some
    exception handler is specified.
  • Exception handlers are used to recover a
    programs execution (and avoid termination) when
    an exception has been raised.
  • PL/I had a primitive form of exception handling
    with its ON conditions that specified an action
    to take when an error of a given type occurred.
    ON conditions were executable statements
    (providing dynamic scope) and any state repair
    took place in the referencing environment in
    effect when the ON was executed rather than when
    the exception occurred.
  • John Goodenough published several papers
    concerning structured exception handling and some
    of his ideas were implemented in the CLU
    programming language. It allowed any block to
    contain an exception handler which would list
    named exceptions and associated actions. Raising
    an exception terminates the current block and
    initiates execution of the associated exception
    handler in that block. Unhandled exceptions are
    raised again in the calling context.

6
How is an Exception Represented
  • Exceptions may or may not contain data.
  • Exceptions may or may not be arranged in a class
    hierarchy.
  • Ada exceptions are arranged in a class hierarchty
    but have no data associated with them.
  • The Dylan language provides a class hierarchy of
    conditions, some of which contain data and some
    of which do not.
  • C allows any class to be treated as an
    exception class.

7
Handling Exceptions
  • The most common approach to exception handling
    nowadays is to use a try-catch block similar to
    that of CLU.
  • The try clause specifies the normal execution.
    If execution of the try clause raises an
    exception, then an object corresponding to the
    exception is created and the try clause
    terminates.
  • Each catch clause can handle one type of
    exception. If an exception has been raised, the
    clause trapping the type of exception raised by
    the try will be executed.Note this means that
    the language must be able to determine the class
    to which the raised exception belongs. This
    requires some form or run-time type
    identification (RTTI) mechanism to be provided.
  • If an exception is not handled by a catch block,
    it is raised again in the context containing the
    try-catch, and is handled by the nearest
    dynamically enclosing try-catch.
  • If an exception is handled by a catch block, all
    major languages nowadays terminate the try-catch
    after the catch completes. This behavior (known
    as the termination model) has completely
    overwhelmed the PL/I behavior (known as the
    resumption model).

8
Finalization
  • The term finalization is used to describe a block
    of code that is to be executed either on
    termination of a block or when an object is
    deleted.
  • Java provides a finally clause for its try-catch
    blocks that will execute on exit from the block
    no matter whether exit is from the try block or a
    catch block. The finally clause may close files,
    or perform other cleanup
  • Those languages that provide finalization of
    objects do not need to perform object clean-up in
    an exception handler finalization clause. Such
    clean-up will be handled as the objects
    themselves go out of scope.
  • Other forms of finalization may be required for a
    block, but Ive yet to see a good example. If
    you ever find one, please send it to me.
Write a Comment
User Comments (0)
About PowerShow.com