Exception handling - PowerPoint PPT Presentation

About This Presentation
Title:

Exception handling

Description:

That is, the occurrence is an exception to the normal sequencing of events. ... caused by an invalid array subscript, the translator inserts instructions at ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 11
Provided by: borameCs
Category:

less

Transcript and Presenter's Notes

Title: Exception handling


1
Exception handling
  • Programming Language Design and Implementation
    (4th Edition)
  • by T. Pratt and M. Zelkowitz
  • Prentice Hall, 2001
  • Section 11.1

2
Exception handling
  • Events in a program sometimes occur at
    unpredictable times, I.e., apparently randomly.
  • That is, the occurrence is an exception to the
    normal sequencing of events. Such events are
    called exceptions, and programming language
    constructed designed to handle these are called
    exception handlers.
  • Exceptions can be created by the hardware or by
    software
  • Examples
  • Power failure Hardware generated
  • Printer out of paper
  • End of page
  • Divide by 0
  • End of array reached Software generated

3
Testing for exceptions
  • Although software exceptions can be tested in the
    code, the existence of an exception handling
    mechanism relieves the programmer the burden of
    constantly checking for this condition.
  • Causing an exception to raise an exception or
    throw an exception.
  • Example End-of-page
  • Without exceptions, need to test at each print
    statement
  • print data
  • if end-of-page then call newPage()
  • With automatic exceptions, setup mechanism
  • PL/I example On endpage begin
  • put page
  • end / Resume execution /
  • can write output without checking end-page
    condition each time

4
Implementation of exceptions
  • Exceptions come from two sources
  • conditions detected by the virtual machine, and
  • conditions generated by the semantics of the
    programming language.
  • In the former case, operating system exceptions
    may be raised directly by hardware interrupts or
    traps, such as arithmetic overflow, or they may
    be raised in support software, such as
    end-of-file condition.
  • In C, the programmer has direct access to these
    signals processed by the operating system. The
    programmer may enable an interrupt (e.g., the
    sigaction function in Unix, which specifies a
    procedure that gets invoked when the given signal
    is raised).

5
Exceptions in ML
  • One can throw an exception and a handler will
    catch the exception. (More limited than PL/I
    example. Need to explicitly raise an exception.)
  • fun doDivide(a,b) if b0 then raise
    badDenominator
  • else a/b
  • fun divide(a,b) doDivide(a,b)
  • handle badDenominator gt (print Divide error
    0.0)
  • Note
  • doDivide is within dynamic scope of divide.
  • Semantics if exception is raised
  • - If no handler, end function
  • - Check dynamic scope of executing procedures
    to look for appropriate handler
  • - If handler, execute handler, then return to
    exception point (e.g., print message and return
    0.0 as default value in above example.)

6
Exceptions in C
  • try clause implements exceptions.
  • Similar to ML
  • try
  • statements . . . If b0 throw badDenominator
  • catch badDenominator Do something
  • Propagating exceptions
  • If no local exception handler, who catches
    exception?
  • Usually dynamic links checked for handler
  • try statement terminates after handling exception.

7
Exceptions in Ada
  • procedure Sub is
  • Bad_Data_Value exception
  • -other declarations for Sub
  • begin
  • -statements for normal processing in
    Sub
  • raise Bad_Data_Value -- to raise this
    exception
  • exception
  • when Bad_Data_Value gt
  • -handler for bad data values
  • when Constraint_Error gt
  • -handler for predefined
    exception Constraint_Error
  • when others gt
  • -handler for all other
    exceptions
  • end
  • Constraint_Error (e.g., number too large) is a
    built-in exception in Ada.

8
Propagating exceptions
  • the place at which an exception occurs is not the
    best place to handle it
  • a particular exception is usually defined in
    terms of the dynamic chain of subprogram
    activations
  • each activation record needs to record those
    active exceptions being passed from calling
    procedure
  • propagating exceptions allows a subprogram to
    remain as a programmer-defined abstract operation
    even in processing exceptions
  • a subprogram may interrupt its normal processing
    and raise an exception.
  • to the caller, the effect of a subprogram's
    raising an exception is the same as a primitive
    operation's raising an exception if the
    subprogram does not handle the exception.
  • if the exception is handled within the
    subprogram, then the subprogram returns in the
    normal way, and the caller is never aware that an
    exception has been raised.

9
More on implementation of exceptions
  • the language translator may insert additional
    instructions. For example, to detect Index_Check
    caused by an invalid array subscript, the
    translator inserts instructions at each reference
    to an array, such as AI,J, to determine whether
    the values of I and J are within the declared
    bounds.
  • Unless the hardware or operating system provides
    the exception checking, checking for an exception
    requires software simulation. Often this cost is
    large.
  • For example, it may take longer to perform the
    subscript bounds check on AI,J than it does to
    access the element of the array. Because of this
    extra cost, most languages provide a means to
    turn off checking for exceptions in parts of the
    program where the programmer determines it is
    safe to do so e.g., pragma Supress(Index_Check)
    in Ada.
  • Note programmers are notoriously bad in
    determining when it is safe to do so.

10
Assertions
  • An assertion is a predicate that checks for an
    invalid value and then raises an exception
  • In C
  • include ltassert.hgt
  • . . .
  • assert(x,y)
  • In C it is really a macro that generates
  • if (xgty) / Print error message /
  • (Compare assertions to pre- and postconditions of
    axiomatic verification in Section 4.2.4.)
Write a Comment
User Comments (0)
About PowerShow.com