最后块重置堆栈跟踪 [英] Finally block resets stacktrace

查看:47
本文介绍了最后块重置堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了Final块问题.我在CatchAndRethrowImplicitly2()中的try-catch-finally代码需要重新抛出异常,如果没有finally块,则stacttrace内容确实包含原始异常信息,但是,如果包含了Final块以清理资源,则该异常原始异常的堆栈跟踪消失了.这是我的代码:

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");
            }
        }
    }

推荐答案

这是我期望发生的事情.由于finally块正在清除状态,并且可以调用其他方法(也可能抛出),因此堆栈无论如何都是无效的. catch块在finally块之后而不是之前执行.
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.


解决方案2提到.如果要处理或记录异常,为什么不在catch块内进行处理,而最后进行清理.

如果要重新引发原始异常,请使用using语句.请在此处查看[
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[^].


您不能这样做吗?

Can''t you just do this?

Exception saved = null;
try
{
}
catch (Exception ex)
{
    saved = ex;
}
finally
{
}
throw saved;


这篇关于最后块重置堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆