今天在WPF程序中加了UnhandledException的处理,参考这篇文章:https://code.msdn.microsoft.com/windowsdesktop/Handling-Unhandled-47492d0b/sourcecode?fileId=67148&pathId=726068521
结果发现CurrentDomain_FirstChanceException这个抛出了很多异常,把相应的异常过滤下:
void CurrentDomain_FirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
{
if (e.Exception is System.DllNotFoundException||
e.Exception is System.IO.FileNotFoundException ||
e.Exception is System.IO.IOException ||
e.Exception is System.Management.Automation.Host.HostException ||
e.Exception is System.Management.Automation.ItemNotFoundException||
e.Exception is System.Management.Automation.PSInvalidCastException||
e.Exception is System.Management.Automation.PipelineStoppedException||
e.Exception is System.Management.Automation.CmdletInvocationException||
e.Exception is System.Management.Automation.PSArgumentException)
{
return;
}
MessageBox.Show("1. CurrentDomain_FirstChanceException" + e.Exception);
//ProcessError(e.Exception); - This could be used here to log ALL errors, even those caught by a Try/Catch block
}这个是调用Powershell脚本时出现的异常,都忽略掉。
为什么说”原来WPF默认处理了这么多Exception“?因为我没加这段代码前,整个程序是不报错的,加了这个代码之后才有了异常。
我为什么要加这段代码呢?因为我在长时间运行WPF程序之后,出现了“XXX has stopped working”,这说明有异常WPF没有处理掉,想加这个调查下到底哪个异常没有处理导致程序Crash,希望接下来可以看到root cause。
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/yangzhenping/article/details/49739141