module 5 pl i

4
Exception Handling Exception handling (EH) allows a programmer to provide code in the program to handle run-time errors or exceptional situations this improves reliability EH was first introduced in PL/I and found in few languages (except for EOF-types of mechanisms) until C++ EH has been included in most modern languages since C++ since their utility greatly improves reliability Here, we look at PL/I, Ada, C++ and Java and briefly consider Events as a special form of exception design issues: how/where are exception handlers specified and what is their scope? how is an exception bound to a handler? where does execution continue after the handler executes (continuation) how are user-defined exceptions specified, if at all? are there predefined exceptions? If so, should there be default handlers for them? can predefined exceptions be explicitly raised? are hardware errors treated as exceptions to be handled? should it be possible to disable predefined exceptions?

Upload: nevinvv

Post on 14-Feb-2016

213 views

Category:

Documents


0 download

DESCRIPTION

PL/I principle of programming languages

TRANSCRIPT

Page 1: Module 5 PL I

Exception Handling• Exception handling (EH) allows a programmer to provide code in the

program to handle run-time errors or exceptional situations– this improves reliability

• EH was first introduced in PL/I and found in few languages (except for EOF-types of mechanisms) until C++

– EH has been included in most modern languages since C++ since their utility greatly improves reliability

• Here, we look at PL/I, Ada, C++ and Java and briefly consider Events as a special form of exception

– design issues:• how/where are exception handlers specified and what is their scope?• how is an exception bound to a handler?• where does execution continue after the handler executes (continuation)• how are user-defined exceptions specified, if at all?• are there predefined exceptions? If so, should there be default handlers for

them?• can predefined exceptions be explicitly raised?• are hardware errors treated as exceptions to be handled?• should it be possible to disable predefined exceptions?

Page 2: Module 5 PL I

PL/I• User-defined exception handlers can appear anywhere in the program (see

below)– condition is a pre-defined or user-defined exception

• two built-in conditions are ANYCONDITION and ERROR– user-defined exceptions work as follows:

• ON CONDITION (boolean)– SNAP is a keyword to print out dynamic chain

• Reference environment is the code in which handler is embedded• Binding is dynamic: the handler is bound to the exception from the ON

statement until either – a new handler is defined– the end of the local block is reached– a revert statement is reached

• Built-in exceptions all have built-in handlers but can be overridden by user-defined handlers – you can enable or disable conditions (some default to being disabled)

• (NO condition) : statement; or• (condition) : statement;

• Continuation can be – a branch (using GO TO)– continue with the same or next instr.– termination of program

General form: ON condition [SNAP] BEGIN;

… END;

Page 3: Module 5 PL I

PL/I Examples

Above, SIZE is enabled, from the On instruction forward, a handler is defined for any SIZE error (the error handler simply does “SNAP”). The SIZE error handler continues to Operate in B, C, D and E because of dynamic binding (even though only C is inside of B’s static scoping)

In the example below, handlers forEndfile are provided for two different files

Built-in exceptions include ENDPAGE, ENDFILE, CONVERSION, OVERFLOW, UNDERFLOW, ZERODIVIDE, SIZE, STRINGRANGE, SUBSCRIPTRNAGE, UNDEFINEDFILE

Page 4: Module 5 PL I

Ada Example

with Ada.Text_IO, Ada.Integer.Text_IO; use Ada.Text_IO, Ada.Integer.Text_IO; procedure Grade_Distribution is Freq : array(1..10) of Integer := (others => 0); New_Grade, Index, Limit_1, Limit_2 : Integer; begin loop Get(new_Grade); Index := New_Grade / 10 + 1; begin Freq(Index) := Freq(Index) + 1; exception when Constraint_Error =>

if New_Grade = 100 then Freq(10) := Freq(10) + 1; else Put_Line(“Error – new grade is out of range”); end if;

end; end loop;

exception when End_Error => Put(“Limits Frequency”); for Index in 0..9 loop Limit_1 := 10 * Index; Limit_2 := Limit_1 + 9; if Index = 9 then Limit_2 := 100; end if; Put(Limit_1); Put(Limit_2); Put(Freq(Index+1)); New_Line; end loop; end Grade_Distribution;