Having a destructor take different actions depending on whether an exception occurred

Posted by dan04 on Stack Overflow See other posts from Stack Overflow or by dan04
Published on 2010-04-09T07:02:47Z Indexed on 2010/04/09 7:13 UTC
Read the original article Hit count: 343

Filed under:
|
|
|

I have some code to update a database table that looks like

try
{
   db.execute("BEGIN");
   // Lots of DELETE and INSERT     
   db.execute("COMMIT");
}
catch (DBException&)
{
   db.execute("ROLLBACK");
}

I'd like to wrap the transaction logic in an RAII class so I could just write

{
   DBTransaction trans(db);
   // Lots of DELETE and INSERT
}

but how would I write the destructor for it?

© Stack Overflow or respective owner

Related posts about c++

Related posts about raii