Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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:
 
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 2-Mar-12 7:59am
Edited 2-Mar-12 8:34am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Mohammad A Rahman at 2-Mar-12 19:38pm
   
To the point :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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[^].
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Can't you just do this?
 
Exception saved = null;
try
{
}
catch (Exception ex)
{
    saved = ex;
}
finally
{
}
throw saved;
  Permalink  
Comments
inbox_aa at 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)

  Print Answers RSS
0 OriginalGriff 250
1 George Jonsson 175
2 Jochen Arndt 150
3 Kornfeld Eliyahu Peter 149
4 PIEBALDconsult 110
0 OriginalGriff 6,080
1 DamithSL 4,648
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,624
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 2 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100