Modula-2 Reloaded

A Modern Typesafe & Literate Programming Notation

Site Menu

Project

Specification

Implementation

Recommendations

Reference

Needs Updating

Work in Progress

Wastebasket

Wiki Manual

edit SideBar

Local Exception Handling

PROCEDURE Foobar( p1, p2, p3 : T ) : Status;
BEGIN

IF foo = NIL THEN
    BAILOUT(FooNilError); (* raise local exception *)
ELSE
     doFooStuff;
END;

DoBar(bar);
IF bar = 0 THEN
    BAILOUT(BarZeroError); (* raise local exception *)
ELSE
    doBarStuff;
END;

DoBaz(baz);
IF baz > MAXBAZ THEN
    BAILOUT(BazOverflowError); (* raise local exception *)
ELSE
    doBazStuff;
END;

RETURN Success;

(* handle local exceptions *)
ON BAILOUT
   BazOverflowError :
      UndoBarStuff;
   BarZeroError :
      UndoFooStuff;
   FooNilError :
      RETURN Failure;
END Foobar;

Restrictions:

  • a procedure can only have one ON BAILOUT section
  • if present, the ON BAILOUT section must be at the bottom
  • BAILOUT() cannot be called from within the ON BAILOUT section
  • once inside the ON BAILOUT section, program control cannot be transferred back to the procedure body

Pros:

  • increases clarity by emphasising the normal (error-free) flow of a program
  • helps to avoid cache misses due to large sections of inlined error handling code
  • does not incur any cost in the general case (when no exception is raised)
  • raising an exception incurs minimal cost (typically one machine cycle only)
  • very easy to implement (a BAILOUT() call is translated into a JUMP instruction)

Cons:

  • adds an additional feature