小编Dea*_*ash的帖子

什么时候尝试捕获不是尝试捕获?

我有一个有趣的问题,在应用程序关闭期间,try/catch块似乎在堆栈中被忽略.

我没有一个正常工作的测试项目(但由于截止日期,否则我会完全尝试重复此操作),但请考虑以下代码片段.

class IndexNotFoundException : Exception { }

public static string RunAndPossiblyThrow(int index, bool doThrow)
{
    try
    {
        return Run(index);
    }
    catch(IndexNotFoundException e)
    {
         if(doThrow)
             throw;
    }
    return "";
}

public static string Run(int index)
{
    if(_store.Contains(index))
        return _store[index];
    throw new IndexNotFoundException ();
}

public static string RunAndIgnoreThrow(int index)
{
    try
    {
        return Run(index);
    }
    catch(IndexNotFoundException e)
    {
    }
    return "";
}
Run Code Online (Sandbox Code Playgroud)

在运行期间,这种模式非常有用.我们得到了依赖于程序控制异常(坏)的代码的遗留支持,我们可以继续前进并慢慢删除用于程序控制的异常.

但是,当关闭我们的UI时,我们会看到从"运行"抛出的异常,即使"doThrow"对于"RunAndPossiblyThrow"的所有当前使用都是假的.我甚至通过修改代码看起来像"RunAndIgnoreThrow"来验证这一点,我仍然会在UI关闭后崩溃.

Eric Lippert先生,我每天都在阅读你的博客,我很乐意听到这是一些已知的bug,我不会发疯.

编辑这是多线程的,我已经验证所有对象在被访问时都没有被修改

编辑明确显示异常是我们的

编辑忘了提,这是关闭,不幸的是视觉工作室无法直接赶上崩溃.它可能会崩溃在UI线程以外的线程上,一旦主要关闭,它就会关闭.我只能通过反复运行和关闭应用程序来调试它,任务管理器打开,"创建转储文件"并查看Windbg中产生的400 + mb混乱.Win7 64供参考.确保这对你有意义.

编辑

关闭时的以下代码仍显示相同的异常.

class IndexNotFoundException : Exception { } …
Run Code Online (Sandbox Code Playgroud)

c# exception

5
推荐指数
1
解决办法
1024
查看次数

标签 统计

c# ×1

exception ×1