Scope guards allow executing statements at certain conditions if the current block is left:
scope(exit)will always call the statements
scope(success)statements are called when no exceptions have been thrown
scope(failure)denotes statements that will be called when an exception has been thrown before the block's end
Using scope guards makes code much cleaner and allows resource allocation and clean up code to be placed next to each other. These little helpers also improve safety because they make sure certain cleanup code is always called independent of which paths are actually taken at runtime.
scope feature effectively replaces the RAII idiom
used in C++ which often leads to special scope guard objects
for special resources.
Scope guards are called in the reverse order they are defined.