Defensive Use of Construction and Destruction

My personal prejudice is against defensive coding as I feel that it makes code less readable. However, there are certain techniques that I think make the code more readable and less prone to errors. Consider the following code:

  {
    CriticalSectionType Lock;
    Runable[ Priority ].push_tail( this );
  }

The use of an automatic object enables us to assert that the critical section will be exitted upon any return from the enclosing scope.

Writing the same fragment in 'C' requires explicit exitting of the critical section. This opens the possibility of the code not exitting the critical section before returning. This may be desired, but in most cases it will not be.

Similar code can be written for ensuring that other resources are freed upon return from a function that uses them only for its scope or less. This can be particularly useful for rendering functions that access several files more readable. These often have several failure conditions, so have to either have several clean-up blocks, or have generic clean-up code and use the (widely) dreaded goto.