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
{
throw;
}
finally
{
dr.Close();
}
}
void CatchAndRethrowImplicitly()
{
try
{
ThrowException();
}
catch (Exception e)
{
throw;
}
}
void ThrowException()
{
throw new ArithmeticException("illegal expression");
}
}
}