Language Reference/Exception Handling

From wiki.visual-prolog.com

< Language Reference

Revision as of 15:37, 15 September 2008 by Thomas Linder Puls (talk | contribs) (→‎Exception Handling)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The basic part of the exception handling system is based on the two built-in predicates errorExit/1 and trap/3.

  • errorExit raises an exception
  • trap sets an exception handler for a certain computation.

When errorExit is called the currently active exception handler is invoked. This exception handler is executed in its original context, i.e. in the context where it was set rather than in the context where the exception is raised.

The argument that errorExit is invoked on is transferred to the exception handler. This argument must somehow provide the needed description of the exception.

Together with additional runtime routines, it is possible to build high-level exception mechanisms on top of this system.

It is however out of the scope of this document to describe runtime system access routines.

It is likewise out of the scope of this document to describe how the runtime system deals with exceptions occurring inside the runtime system.

The first argument of trap is the term to execute with new exception handler. The second argument must be a variable. This variable will be bound to the value errorExit is invoked on, if it is invoked while this exception handler is active. The third argument is the exception handler, which will be invoked if errorExit is called while this exception handler is active.

The exception handler can access the variable stated in the second argument thereby examining the exception that was raised.

Example

clauses
   p(X) :-
       trap( dangerous(X), Exception, handleDangerous(Exception)).

If an exception is raised while executing dangerous, then Exception will be bound to the exception value, and control will be transferred to the third argument of trap. In this case Exception is passed to handleDangerous.