An exception is a control mechanism that allows the programmer to abort the execution of one phrase and start the execution of another.
To demonstrate the use and semantic definition of exceptions we'll introduce them to the language IMP. We do this with a new set of identifiers used only in exceptions:
We will represent these identifiers with the generic name
We also add two new program phrases:
raise exc exception exc in p1 orelse p2
In the second phrase, any call of
raise exc in
p1 would abort the execution
and start executing
exc is local to
For the semantic definition we extend the relational semantics definition
We first change the set
Results so that a program may evaluate to a memory or an
exception identifier and a memory, i.e.
we can have
With that done that we move on to give the rules for exceptions.
The raising of an exception is quite simple.
For each program phrase which is a composite containing other program phrases, we introduce rules which allow the aborting of execution of the sub-phrases. As an example, for the sequence phrase we add the following new rules to the existing ones:
Finally we add the rules for the exception handling. Note the need for case (3) below where the raised exception does not match that which is handled.
R. Burstall. Language Semantics and Implementation. Course Notes. 1994.
Copyright © 2014 Barry Watson. All rights reserved.