unwind_protect

unwind_protect — ensures code will be evaluated, despite errors in the body code.

Syntax

unwind_protect (!body, !protected_body)

		

Arguments

body

Any Gamma or Lisp expression.

protected_body

Any Gamma or Lisp expression.

Returns

The result of evaluating the protected_body code. If an error occurs then this function does not return.

Description

This function ensures that a piece of code will be evaluated, even if an error occurs within the body code. This is typically used when an error might occur but cleanup code has to be evaluated even in the event of an error. The error condition will not be cleared by this function. If an error occurs then control will be passed to the innermost trap_error function or to the outer level error handler immediately after the protected_body is evaluated.

Example

The following code will close its file and run a write_all_output function even if an error occurs.

if (fp=open("filename","w"))
{
	unwind_protect(write_all_output(),close(fp));
}
		

See Also

error, trap_error, protect unwind