3

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);
            }
        }
    }
1
  • I'd handle the transaction rollback in the database (for example by using a stored procedure for updating / inserting data). So you don't have to try to rollback a transaction inside a catch block when inserting / updating failed in the first try block. (o.O) Commented Apr 13, 2016 at 14:02

3 Answers 3

4

I personally wouldn't add a try catch block in the finally, then it can be an endless chain. Usually you shouldn't have complicated stuff in the finally and in any case unexpected exception should be caught in the caller.

Edit: looking a bit closer to the code, I don't see why the code in the finally shouldn't be in the try block.

Sign up to request clarification or add additional context in comments.

2 Comments

I appreciate wanting to keep complexity out and simplicity in with regards the finally block. However, in the scenario where the system is trying to ensure that a specific piece of functionality is done come hail rain or shine, would the try catch be acceptable in the finally block?
if you must, may be refactor that code into a method with it's own try-catch block and use that method in the finally. But don't panic, the caller should catch all unexpected exception, including in the finally block
0

I might be being overly paranoid, but I find myself very worried about possible unhandled exceptions that may occur with these.

Don't panic. If there is an error in the db layer try catch it there.

Comments

0

The most dangerous thing in the original code is that if it fails and the transaction rollback fails, the only error rethrown will be the one from rolling back the transaction. You'll never know what the original exception was that caused you to have to roll back the transaction. That's the one that you probably care most about.

If you want to be paranoid, log the failure of the transaction rollback. But what matters is the previous exception that got you to that point. That should either be logged or rethrown depending on the expectation of the caller.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.