Be careful about exception after resource allocation

The following is a common code pattern         Resource resource = GetResource();         DoWork();                    return resource; If DoWork() throws exception, the resource will be leaked. We need to guard against this. For example         bool success = false;         Resource resource = GetResource();         try         {             DoWork();             success = true;            …

6