为什么ASP.NET会抛出这么多异常?

30

恰巧我看了一下Visual Studio的调试输出。我可以看到数以百计的各种异常被抛出。我检查了另一个基于ASP.NET的解决方案,它显示了相同的行为。为什么会抛出所有这些异常?我不相信这对整体性能有好处,是吗? 请看下面的摘录。 这是大约30秒冲浪的输出。 大多数是HttpExceptions,但也有FormatExceptions和ArgumentOutOfRangeExceptions。 这些都没有真正影响使用。 没有崩溃。 有人能解释一下吗,因为它似乎是“正常”的吗?

A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
'w3wp.exe' (Managed): Loaded 'C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\a402e511\e6aaa0de\App_Web_vdj_eurz.dll', Symbols loaded.
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
A first chance exception of type 'System.InvalidCastException' occurred in mscorlib.dll
A first chance exception of type 'System.InvalidCastException' occurred in mscorlib.dll
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll

1
如果您重新构建网站,它是否能正常工作? - Tom Gullen
不,我的意思是所有东西都能正常运作。只是想知道那些异常的含义。 - Krumelur
我看到你接受了一个答案。我仍然很好奇:这种行为是正常的吗,还是因为你的代码引起的? - R. Martinho Fernandes
是的,我的代码引起了它们。这与在大型项目中最终需要的所有那些小的try-catch块有关。其中一些我可以通过简单地添加一个NULL检查而不是try-catch来修复。其他的则按预期工作,例如尝试使用反射重连事件处理程序。 - Krumelur
故事的寓意是要摆脱那些 try/catch 块,这样你就可以快速找出代码的问题并加以修复。 - John Saunders
4个回答

39

我不确定到底是什么原因导致了这个问题,但我知道您可以找出原因(更好的是,对吧?:))。

在Visual Studio中:

  • 点击“调试”菜单
  • 点击“异常…”
  • 在“公共语言运行时异常”下选择“抛出”

这将使Visual Studio在任何异常发生时暂停调试器,以便您可以检查实际发生的情况。(不会影响生产代码,只会在VS中暂停)

顺带一提,您可能想要实现类似自动异常邮件等功能,以从生产站点获取更多信息。

请注意,您可能无法默认在VS中捕获所有异常(一次性异常有时可能有点难以捉摸)。如果您想更加严格,可以让Visual Studio调试器调试.NET Framework。您的目标应该是看到完整的异常+堆栈跟踪,在大多数情况下,这将告诉您出了什么问题。

一个好的做法是摆脱异常(找出原因并解决),而不是隐藏或忽略它们。

编辑

值得注意的是,“一次性异常”是可能会被try-catch捕获的异常。.Net中的一些内部函数在满足某些条件时会抛出异常,这是Visual Studio/调试工作方式的一部分,并不意味着有东西崩溃了。Visual Studio会为您记录这些异常,以防您需要它们,但这并不意味着您必须对它们采取行动。


2
46秒。这就是我制作快照所需的时间。 - user1228
谢谢!我能找出导致问题的原因了。不幸的是,这些Http异常已经停止了。我会检查它们是否会再次出现。 - Krumelur
谢谢。现在我知道了:“提供程序与Oracle客户端版本不兼容”。直到我勾选了“CLRE”,VS2013才有所作为。 - JoshYates1980
2
在VS2015中:点击菜单调试(Debug),子菜单窗口(Windows),项目异常设置(Exception Settings)(快捷键Ctrl-Alt-E),可以获取一堆可以检查的异常。 - Roland

25

谁知道呢?

但是你可以找出答案。按下ctrl-alt-E,在异常对话框中指示调试器在抛出异常时中断:

exceptions dialog

当程序出现错误时,检查InnerException属性以查看导致HttpException的原因。


5

啊哈。这告诉我这不是问题,异常会以某种方式被处理。但它确实显示了System.Web.dll和mscorlib.dll内部出现了错误? - Krumelur
那么,这种行为正常吗?即使是ArgumentOutOfRange的情况?捕获ArgumentOutOfRange闻起来像是基于异常的控制流...他们在mscorlib内部使用吗?哎呀!@Krumelur你确定你没有自己捕获它们吗? - R. Martinho Fernandes
是的,第一次机会异常是已经被抛出并且被处理的异常。 - Oded

2

额外(可能有用的)信息:

造成这种情况的原因很多,其中之一可能是该站点/网络应用程序没有favicon.ico文件。

如果实际异常是:System.Web.StaticFileHandler.GetFileInfo,显然是找不到文件。如果您在浏览器上没有看到实际的404错误,则很可能是因为浏览器在找不到favicon.ico时会静默地继续执行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接