Redoundant code in exception handling

Posted by Nicola Leoni on Stack Overflow See other posts from Stack Overflow or by Nicola Leoni
Published on 2010-06-11T10:09:17Z Indexed on 2010/06/11 10:12 UTC
Read the original article Hit count: 270

Filed under:

Hi, I've a recurrent problem, I don't find an elegant solution to avoid the resource cleaning code duplication:

resource allocation:

try {
  f()
} catch (...) {
  resource cleaning code;
  throw;
}

resource cleaning code;
return rc;

So, I know I can do a temporary class with cleaning up destructor, but I don't really like it because it breaks the code flow and I need to give the class the reference to the all stack vars to cleanup, the same problem with a function, and I don't figure out how does not exists an elegant solution to this recurring problem.

© Stack Overflow or respective owner

Related posts about c++