C# - 无限循环的异常抛出? [英] C# - Infinite Loop at Exception Throwing?

查看:591
本文介绍了C# - 无限循环的异常抛出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

protected void ExecuteInTransaction(Action action)
{
    using (SQLiteTransaction transaction = connection.BeginTransaction())
    {
        try
        {
            action.Invoke();
            transaction.Commit();
        }
        catch(Exception)
        {
            transaction.Rollback();
            throw;
        }
    }
}

在测试这个类,我设法以测试捕获抛出一个异常分支的。

正如我在调试模式,我继续这种投掷的执行,看的调用类如何处理它的,但例外的是永远抛出法,相反,它就像例外是不断被抛出和捕获,抛出和捕获一遍,从来没有退出函数

As I'm in Debug mode, I continue the execution of this throwing, to see how the calling class handles it, but the exception is never thrown by the method, instead, it's like the exception is constantly being thrown and caught, thrown and caught all over again, never exiting the function.

发布模式,应用程序冻结,停止工作:

In Release mode, the application freezes and stops working:

有谁知道为什么发生这种情况,我怎么能避免呢?

Does anybody know why this is happening, and how can I avoid it?

在此先感谢!

推荐答案

没有无限循环。 Visual Studio的只是停止在未捕获的异常将中止程序的地方。 试图继续不做任何事情,因为没有什么进一步的执行(VS只是再次显示相同的消息,提醒你)。

There is no infinite loop. Visual Studio just stops at the place where the uncaught exception would abort the program. Trying to continue does nothing because there is nothing further to execute (VS just displays the same message again to remind you of that).

如果你有一些调用函数一个try / catch处理程序,你就可以调试到那里。 (但如果catch处理程序再重新抛出,VS会停在那里了。)

If you had a try/catch handler in some calling function, you would be able to debug into there. (But if that catch handler rethrows again, VS would stop there, too.)

请注意, SQLiteTransaction 自动处理回滚时,打开的事务设置;它的目的是让你的code可以是简单的:

Please note that SQLiteTransaction automatically handles rolling back when an open transaction is disposed; it is designed so that your code can be simpler:

protected void ExecuteInTransaction(Action action)
{
    using (var transaction = connection.BeginTransaction())
    {
        action.Invoke();
        transaction.Commit();
    }
}

这篇关于C# - 无限循环的异常抛出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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