next up previous contents
Next: Conditional Statements Up: Statements Previous: Assignments

Blocks and Exception Handling

  
 begin_block ::= 'begin' block [ exceptions ] 'end' .
  exceptions ::= 'except' exception_identifier
                 ( 'when' exception_types 'then' block )\stern
                   [ 'else' block ] .
  exception_identifier ::= identifier .
  exception_types ::= exception_type | exception_types ',' exception_type .
  raise_statement ::= 'raise' [ exception_type ] .
  exception_type ::= type_specifier .
  assertion_statement ::= 'assert' expression .

Example: begin i:INT; i:=f(3); ...end
begin a := b / c except e when ZERO_DIVIDE then a := 1000 end; tex2html_wrap_inline1707

A block is a sequence of statements possibly preceded by local declarations. Any locally declared identifier shadows non-local entities, cf. 5.1. The life-time of local variables and constants ends when the execution of the block terminates. Method bodies, parts of conditional case and typecase statements, the typecase statement as a whole and loop bodies all constitute blocks. begin-blocks may be used for further subdividing the scopes and life-times of entities and for exception handling.

A begin-block containing exceptions is called an exception statement. It is processed by first executing the initial declarations and statements. If during this processing no explicit or implicit raise-statement is executed the processing of the exception statement is finished.

If a raise-statement occurred it must have specified a class type C as its argument which is a conforming to the predefined class EXCEPTION. A new object o of class C is created and control is transferred to the first exception clause which contains a supertype T of C in its exception_types. If such an exception clause is found then the corresponding block is prefixed by a local declaration tex2html_wrap_inline2385 and then executed. Then the processing of the exception statement is finished.

If in an exception statement no appropriate exception clause could be found or if the executed exception clause itself executed a raise-statement then the exception statement is terminated abnormally and the search for an exception clause is continued in the dynamically enclosing block.

If also in the enclosing block no conforming exception clause can be found or if a raise-statement occurs outside an exception statement then the processing of the procedure is terminated abnormally and the search is continued at the place of the procedure call.

The execution of the program terminates abnormally if the execution of the procedure main terminates in this way.

A raise-statement must always specify an exception type except when it is used in an exception clause itself; in this case an omitted exception type indicates that the same exception object should be used again, i.e. the same exception as before is raised.

Exceptions can also be raised implicitly. Such implicit exceptions include FLOAT_OVERFLOW, FLOAT_UNDERFLOW, INTEGER_OVERFLOW, ZERO_DIVIDE, INDEX_ERROR, REFERENCE_ERROR, INVARIANT_ERROR, ASSERTION_ERROR, STREAM_TERMINATION, EXIT. The exception type NUMERIC_ERROR is a supertype of all numeric exceptions.

Note: Thus, an exception-type occurring in a raise statement must always be given by a class specifier whereas when handling the exceptions also polymorphic class types are allowed. tex2html_wrap_inline1707

The assertion statement evaluates its boolean expression. If the value is false, an ASSERTION_ERROR is raised. However, the user cannot rely on these checks actually being performed.


next up previous contents
Next: Conditional Statements Up: Statements Previous: Assignments

Martin Trapp
Mon Feb 17 16:49:16 MET 1997