What is the best practice for exception handling within exception handling?
I find myself working on an existing C# (Framework 4.0) system that uses custom objects within the catch and finally blocks throughout most of the Application Server tier of the system.
Consider the following snipped version of a method in this codebase:
public void DoSomeStuff(string sGUID)
{
try
{
// Foo
}
catch (Exception oEx)
{
oExceptions.Add(oEx);
if (oDBConn.NumberOfActiveTrans > 0)
{
oDBConn.Rollback();
}
}
finally
{
oDBConn.DeleteLocksByGUID(sGUID);
}
}
I might be being overly paranoid, but I find myself very worried about possible unhandled exceptions that may occur with these.
As such, would something like the following updated version be an acceptable practice or is there a better way to accomplish the same thing?
public void DoSomeStuff(string sGUID)
{
try
{
// Foo
}
catch (Exception oEx)
{
oExceptions.Add(oEx);
try
{
if (oDBConn.NumberOfActiveTrans > 0)
{
oDBConn.Rollback();
}
}
catch (Exception oEEx)
{
oExceptions.Add(oEEx);
}
}
finally
{
try
{
oDBConn.DeleteLocksByGUID(sGUID);
}
catch (Exception oFEx)
{
oExceptions.Add(oFEx);
}
}
}
tryto rollback a transaction inside acatchblock when inserting / updating failed in the firsttryblock. (o.O)