C# - 无限循环的异常抛出?

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