在的try-catch未处理的异常 [英] Unhandled exception in try-catch

查看:224
本文介绍了在的try-catch未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

try
{
     list = from XElement e in d.Descendants(wix + "File")
            where e.Attribute("Name").Value.Contains(temp.Name) &&
            e.Parent.Parent.Attribute("Name").Value.Contains(temp.Directory.Name)
            select e;
}
catch (NullReferenceException e)
{
     MessageBox.Show(e.Message);
}
catch (Exception e)
{
     MessageBox.Show(e.Message);
}

现在我的问题是,为什么这个代码产生一个运行时错误说我有一个NullReferenceException未处理。如果您需要了解该计划的更多信息,让我知道

now my question is why does this code produce a run time error saying I have a NullReferenceException unhandled. If you need more information about the program let me know.

编辑:
调试器指向LINQ声明,其中部分。当我运行该程序的exe文件我仍然得到异常的直接

The debugger points to the "where" part of the linq statement. When I run this program direct from the exe file I still get the exception.

推荐答案

编辑:好的,我想我知道了问题...这是由于推迟执行查询。

Okay, I think I know the problem... it's due to deferred query execution.

如果你已经的只是的得到了在try / catch块查询建设,这是不会捕获异常而发生的当执行查询

If you've just got the query construction in the try/catch block, that's not going to catch exceptions which occur while the query is being executed.

看看你有堆栈跟踪,你会发现有将是一个堆栈帧在那里你的执行的查询 - 这只是它来自该位的代码自动生成的lambda表达式,它不是尝试的范围内运行。/ catch块

Look at the stack trace you've got, and you'll find that there'll be a stack frame where you're executing the query - it's just the auto-generated lambda expression which comes from this bit of code, and it's not running within the scope of the try/catch block.

原来的答复:

您只看到在调试器中的例外呢?如果是这样,进入调试异常对话框,并更改在哪些异常导致调试器破点的偏好。 catch块,应处理的NullReferenceException 正常。 (诚​​然,我不认为你确实应该抓住的NullReferenceException 摆在首位,并追赶的所有的这样的异常通常是一个坏主意太,比在堆栈的顶部其他 - 但那是另一回事)

Are you just seeing the exception in the debugger? If so, go into the debugger exception dialog and change the preferences for the point at which exceptions cause the debugger to "break". The catch block should be handling the NullReferenceException normally. (Admittedly I don't think you should really be catching NullReferenceException in the first place, and catching all exceptions like that is generally a bad idea too, other than at the top of the stack - but that's a different matter.)

这篇关于在的try-catch未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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