Wednesday, May 23, 2012

Using finally instead of catch

I've seen this pattern a few times now:



        bool success = false;
try
{
DoSomething();
success = true;
}
finally
{
if (!success)
Rollback();
}


And I've been wondering: Why is this better than using catch for rollbacks?



        try
{
DoSomething();
}
catch
{
Rollback();
throw;
}


What are the differences between the two ways of making sure changes are rolled back on failure?





No comments:

Post a Comment