Exception Handling

Handlers are lexically bound to blocks of code. If an error occurs in the block, control jumps to the handler and the rest of the block is skipped. If exception can’t be handled, the subroutine returns abruptly, and an exception is raised at point of call. If not handled there, exception propagates up the dynamic chain. If not handled in main routine, a predefined outermost handler is invoked.

Implementation of exception handling

  • Stack of handlers; maintain a stack of exception handlers - when you enter a block of code, push the corresponding handler onto the stack. Incurs runtime overhead in the common case. Every protected block and subroutine must have code to push and pop handlers.
  • Table that contains addresses of start/end of blocks of code, along with the exception that corresponds to the code in that block. Use binary search when an exception is thrown to find the corresponding exception in the table. Free in the normal case with no exceptions being thrown. Java has a separate table for each method.

In ML, exceptions are datatypes that are treated specially. In Eiffel, using the design by contract framework, routines called with appropriate preconditions should either fulfill contract or raise exception. Invariants should be restored before exception is propagated.

Object-Oriented Programming

Ch 10 in Scott.

An Abstract Data Type is one that is defined in terms of the operations that it supports rather than in terms of its structure or implementation

Oberon, Niklaus Wirth’s successor to Modula 2, offered type extensions. Add extra fields to a user-defined type; pure form of extensibility.

3 Keys for Success in OOP

  1. Encapsulation
  2. Inheritance
  3. Dynamic method binding

Dynamic method binding refers to when the program doesn’t know at compile time exactly what type the object referred to will be. Do know that it will be a certain declared class, or a subclass of the declared class.

Implementation of virtual functions in C++

  • Virtual functions are passed an extra, hidden, parameter- “this”. In Java, all member functions are virtual; e.g., invokevirtual
  • Each class has dispatch table (vtable)
  • Each object has pointer to its class’s vtable
  • Objects of a subclass have a different dispatch table

The compiler knows the offset of virtual methods in the vtable, so order in the table is important. The fragile base class problem: to invoke a method on an object, must get address of object’s vtable, calculate offset into vtable (can be done at compile time), invoke method at that address. If the base class’s methods are re-ordered, clients will have to be recompiled too! Java solved this somewhat by representing methods in class files by symbolic links. These links are resolved after compile time to find offsets in the method table.

Inheritance

Many languages don’t support multiple inheritance. Complicated to implement.

  • Repeated inheritance- two parent classes share a single grandparent class. In shared inheritance, only one copy of members is made (Eiffel). In C++, default is to make multiple copies.

Mix-in inheritance

Single inheritance from 1 real base class, but can extend many abstract classes.