我有一个有趣的问题,在应用程序关闭期间,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 { }
public static string RunAndPossiblyThrow(int index, bool doThrow)
{
try
{
return Run(index);
}
catch
{
}
return "";
}
public static string Run(int index)
{
if(_store.Contains(index))
return _store[index];
throw new IndexNotFoundException ();
}
Run Code Online (Sandbox Code Playgroud)
似乎摆脱异常的唯一事情是直接去
class IndexNotFoundException : Exception { }
public static string RunAndPossiblyThrow(int index, bool doThrow)
{
try
{
return Run(index);
}
catch
{
}
return "";
}
public static string Run(int index)
{
if(_store.Contains(index))
return _store[index];
return "";
}
Run Code Online (Sandbox Code Playgroud)
当然,例外情况已经消失,但我对疯狂的恐惧仍然存在.
编辑
它变得更糟......这仍然崩溃......
class IndexNotFoundException : Exception { }
public static string RunAndPossiblyThrow(int index, bool doThrow)
{
try
{
throw new IndexNotFoundException();
}
catch
{
}
return "";
}
Run Code Online (Sandbox Code Playgroud)
编辑我有一种明显的感觉,这将使我无处可去.除了奇怪的行为之外,我还可以注意到在上述情况下执行UI期间,try catch正在忠实执行.我的UI没有崩溃,而且它充满了空字符串.但是,一旦我开始关闭UI,崩溃就会显示出来并且try catch不再阻止异常.
编辑和最终显然转储文件在其中列出了最新的第一次机会异常.我通过创建一个新项目来验证这一点,该项目在try catch中睡了10秒钟.在等待期间,我得到了.dmp文件,果然,我完全被捕获的异常出现了.
我将为有用的答案标记一些要点,但不幸的是,我的代码崩溃仍然没有押韵或原因......
有各种无法捕获的异常。特别是堆栈溢出!其中之一可能发生在您的代码中的某个地方吗?
请参阅http://www.bluebytesoftware.com/blog/PermaLink,guid,223970c3-e1cc-4b09-9d61-99e8c5fae470.aspx