25.07.2014 Views

VDM-10 Language Manual

VDM-10 Language Manual

VDM-10 Language Manual

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 13. Statements<br />

result of the complete recursive trap statement. Otherwise, the value is matched in order<br />

against each of the patterns pati. When a match cannot be found, the exception is returned<br />

as the result of the recursive trap statement. If a match is found, the corresponding statement<br />

si is evaluated. If this does not raise an exception, the result value of the evaluation of si is<br />

returned as the result of the recursive trap statement. Otherwise, the matching starts again,<br />

now with the new exception value (the result of the evaluation of si).<br />

Examples: In many programs, we need to allocate memory for a single operation. After the<br />

operation is completed, the memory is not needed anymore. This can be done with the<br />

always statement:<br />

✞<br />

✡✝<br />

( dcl mem : Memory;<br />

always Free(mem) in<br />

( mem := Allocate();<br />

Command(mem, ...)<br />

)<br />

)<br />

In the above example, we cannot act upon a possible exception raised within the body statement<br />

of the always statement. By using the trap statement we can catch these exceptions:<br />

✞<br />

✡✝<br />

trap pat with ErrorAction(pat) in<br />

( dcl mem : Memory;<br />

always Free(mem) in<br />

( mem := Allocate();<br />

Command(mem, ...)<br />

)<br />

)<br />

Now all exceptions raised within the always statement are captured by the trap statement.<br />

If we want to distinguish between several exception values, we can use either nested trap<br />

statements or the recursive trap statement:<br />

✞<br />

DoCommand : () ==> int<br />

DoCommand () ==<br />

( dcl mem : Memory;<br />

always Free(mem) in<br />

( mem := Allocate();<br />

Command(mem, ...)<br />

)<br />

);<br />

117<br />

✆<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!