protect evaluates an expression and then its cleanup forms, however control
leaves the expression.
Syntax
(protect ⟨expr⟩
⟨cleanup⟩...)
Semantics
- The cleanup forms run when
⟨expr⟩returns normally and when control leaves it otherwise: through an exception,return,break,continue, orresume-to. - The value of the
protectexpression is the value of⟨expr⟩, which may beVoidor multiple values. The values of the cleanup forms are discarded. As elsewhere, discarding aResultproduces a warning unless it is discarded explicitly with(let _ = ...), as in the example below. - An exception thrown by a cleanup form propagates as usual.
- Neither
⟨expr⟩nor the cleanup forms are in tail position. protectcompiles tocl:unwind-protect.
Example
(define (process-file path)
(let stream = (need (file:open path)))
(protect (process stream)
(let _ = (file:close stream))
Unit))