Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I encountered an Finally block issue. my try-catch-finally code in CatchAndRethrowImplicitly2() needs to rethrowing an exception, if there is not finally block, the stacttrace content did contains the original exception information, however, if it inlcuded the Finally block to clean up the resources, the exception's the original exception's stacktrace was gone. here is my code:

C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace UsageLibrary
    {
    
    class TestsRethrow
        {
        static void Main()
            {
            TestsRethrow testRethrow = new TestsRethrow();
            testRethrow.CatchException();
            }

        void CatchException()
            {

            try
                {
                CatchAndRethrowImplicitly2();
                }
            catch (Exception e)
                {
                Console.WriteLine("{0}Implicitly 2 specified:{0}{1}",
                   Environment.NewLine, e.StackTrace);
                }
            }

        void CatchAndRethrowImplicitly2()
            {
            SqlDataReader dr = null;

            try
                {
                SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=AdventureWorks2008R2;Integrated Security=True");
                SqlCommand commd = new SqlCommand();
                commd.Connection = conn;
                commd.CommandType = CommandType.Text;
                commd.CommandText = "select [Name] from [AdventureWorks2008R2].[Sales].[Store]";
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                dr=commd.ExecuteReader();
                CatchAndRethrowImplicitly();
                }
            catch //(Exception ex)
                {
                throw;
                }
            finally // <-- if comment this block, stacktrace content is reset.
                {
                dr.Close();
                }
            }
        void CatchAndRethrowImplicitly()
            {
            try
                {
                ThrowException();
                }
            catch (Exception e)
                {
                throw;
                }
            }

        void ThrowException()
            {
            throw new ArithmeticException("illegal expression");
            }
        }
    }
Posted
Updated 2-Mar-12 7:34am
v3

This is what I would expect to happen. Since the finally block is cleaning up state and can call other methods (which may also throw), the stack would be invalid anyway. The catch block gets executed AFTER the finally block, not before it.
 
Share this answer
 
Comments
Mohammad A Rahman 2-Mar-12 19:38pm    
To the point :)
As solution 2 mention. If you want handle or log the exception why don't do inside the catch block and do the clean up in the finally.

If you want to re throw the original exception please use using statement. Please see here[^].
 
Share this answer
 
Can't you just do this?

C#
Exception saved = null;
try
{
}
catch (Exception ex)
{
    saved = ex;
}
finally
{
}
throw saved;
 
Share this answer
 
Comments
inbox_aa 2-Mar-12 16:43pm    
thanks very much for quick response, John.
however, doesn't "throw save;" also reset the exception? please see http://msdn.microsoft.com/en-us/library/ms182268(v=vs.110).aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900