The current documentation for defer says only this about exceptions:
any exception it throws gets propagated
Experimenting a bit reveals two undocumented facts:
The deferred function is called even if the containing closure throws an exception. Then, the exception is rethrown. Thus, this provides an alternative to using try { … } finally { … } in some cases.
If both the deferred function and the containing closure throw an exception, only the latter is propagated. Clearly, it makes no sense to propagate both, but at least the behaviour should be documented.
One more defer behaviour that merits documentation: If a closure contains several defer calls, their functions will be called in the reverse order, from the last to the first one.
The current documentation for
defer
says only this about exceptions:Experimenting a bit reveals two undocumented facts:
try { … } finally { … }
in some cases.Examples:
(I had somehow managed to miss
defer
until now, which is why I felt a need to play with it.)