0

I have the following pseudo-code

try
{
    beginTrans();
    //do some database opertions...
    //PointA
    //do some database opertions...
    commit();
}
catch (const AnyDbException& ex)
{
    rollback();
    //error logging...
}

In above code, everything looks OK. How ever, if there is a non-database related exception thrown, it will not be caught in the catch, and the transaction is NOT committed either.

If I use catch(...), it will catch anything but in that case I will prefer to not to catch it as it should be caught outside.

What can I do?

Tony
  • 632
  • 1
  • 4
  • 18
  • 2
    You could create an object that has a `commit()` function, and if that function's not called before the object is destroyed it rolls back automatically. – tadman Apr 09 '20 at 01:21
  • 1
    That's a good idea! – Tony Apr 09 '20 at 01:21
  • In line with the [C++ RAII philosophy](https://stackoverflow.com/questions/161177/does-c-support-finally-blocks-and-whats-this-raii-i-keep-hearing-about). – tadman Apr 09 '20 at 01:23

0 Answers0