简体   繁体   English

当我在静态构造函数中发生异常时,为什么Visual Studio不显示异常消息?

[英]Why doesn't Visual Studio show an exception message when my exception occurs in a static constructor?

I'm running this C# code in Visual Studio in debug mode: 我在调试模式下在Visual Studio中运行此C#代码:

public class MyHandlerFactory : IHttpHandlerFactory
{
  private static Dictionary<string, bool> myDictionary = new Dictionary<string, bool>();
  static MyHandlerFactory()
  {
    myDictionary.Add("someKey",true);
    myDictionary.Add("someKey",true); // fails due to duplicate key
  }
}

Outside of the static constructor, when I get to the line with the error Visual Studio highlights it and pops up a message about the exception. 在静态构造函数之外,当我到达带有错误的行时,Visual Studio会突出显示它并弹出有关异常的消息。 But in the static constructor I get no such message. 但是在静态构造函数中,我没有得到这样的消息。 I am stepping through line-by-line, so I know that I'm getting to that line and no further. 我逐步走过,所以我知道我已经到了那条线而没有进一步。

Why is this? 为什么是这样?

(I have no idea if that fact that my class implements IHttpHandlerFactory matters, but I included it just in case.) (我不知道我的类实现IHttpHandlerFactory的事实是否重要,但是为了以防万一我将其包括在内。)

This is VS2005, .Net 2.0 这是VS2005,.Net 2.0

Edit: I just want to add, the fact that it's an HttpHandler does seem to matter. 编辑:我只想补充一点,事实上它是一个HttpHandler似乎确实很重要。 As the answers have indicated, the default behavior is to break on the TypeInitializationException rather than the inner exception. 正如答案所示,默认行为是打破TypeInitializationException而不是内部异常。 I tested another example without the HttpHandler and saw that this caused the program to break on the first line that used the class. 我在没有HttpHandler的情况下测试了另一个例子,发现这导致程序在使用该类的第一行中断。 But in this case there's no line in my code to break on, since the class was only called as an HttpHandler specified in my web.config file. 但在这种情况下,我的代码中没有任何行可以打破,因为该类只被调用为我的web.config文件中指定的HttpHandler。 Hence, it didn't break on the exception at all. 因此,它根本没有打破例外。

The problem is that the exception thrown is actually a TypeInitializationException that wraps whatever exception is thrown. 问题是抛出的异常实际上是一个TypeInitializationException,它包装抛出的任何异常。 I'm not sure what design tradeoffs caused this, but IMO it's one of the most annoying things in .NET development, and I'm sad to see it's still around in .NET 4. 我不知道什么样的设计权衡造成这一点,但IMO它是在.NET开发中最头疼的事情之一,我很伤心看到它仍然是围绕在.NET 4。

In VS, to catch the exception ASAP, you'll need to turn on first-chance exceptions. 在VS中,要尽快捕获异常,您需要启用第一次机会异常。 Go to Debug > Exceptions and check "Common Language Runtime Exceptions", which will break as soon as an exception is thrown. 转到Debug> Exceptions并选中“Common Language Runtime Exceptions”,它会在抛出异常时立即中断。

(Note: if you're using the Dynamic Language Runtime, you'll need to be more choosy about what exceptions are caught, since that apparently uses exceptions for flow control). (注意:如果您正在使用动态语言运行时,则需要更加挑剔捕获的异常,因为这显然会使用流控制的异常)。

I tried your code and I got the TypeInitializationException as you expected. 我尝试了你的代码,我得到了你所期望的TypeInitializationException。 No problem in my VS... 我的VS没问题......

But if you run this (or any other) application 'without debugging', you should always get an error message for any unhandled exceptions - no VS settings will make a difference here. 但是如果你在没有调试的情况下运行这个(或任何其他)应用程序,你应该总是得到任何未处理异常的错误消息 - 没有VS设置会在这里产生影响。

The static constructor gets run before your application is running in an appdomain. 在应用程序在appdomain中运行之前,静态构造函数会运行。 This is probably causing issues with the way VS is catching the exception, which from your description is quite non standard. 这可能会导致VS捕获异常的方式出现问题,这与您的描述完全不符合标准。 It may be a limitation of 2005. That's why you should always catch within the body of the static constructor. 它可能是2005年的限制。这就是为什么你应该总是在静态构造函数的主体内捕获。 There is an excellent discussion of this in the new book Effective C# 在新书Effective C#中对此进行了很好的讨论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我的代码在调试器中引发异常时,为什么Visual Studio 2010不向我显示弹出窗口以及引发异常的位置? - Why doesn't Visual Studio 2010 show me a popup and the location where an exception is thrown when my code throws an exception in the debugger? 为什么Activator.CreateInstance在Visual Studio中调试时没有传递构造函数异常? - Why doesn't Activator.CreateInstance pass constructor exception when debugging in Visual Studio? 在 Visual Studio(.NET6 web 应用程序)中调试时发生未处理的异常时,如何减少 Visual Studio 中的错误消息垃圾邮件 - How to reduce error-message spam in visual studio when an unhandled exception occurs while debugging in Visual Studio (.NET6 web application) Visual Studio 2008 - 发生未处理的异常时应用程序关闭 - Visual Studio 2008 - Application closes when unhandled exception occurs 发生未处理的异常时 Visual Studio 监视变量值 - Visual Studio watch variable values when an unhandled exception occurs Visual Studio无法识别我的构造函数? - Visual studio doesn't recognize my constructor? 发生异常后在 Visual Studio 调试器中继续 - Continuing in the Visual Studio debugger after an exception occurs Visual Studio 2010不会停止在Socket.BeginReceive()回调中的未处理异常 - 为什么? - Visual Studio 2010 doesn’t stop at an unhandled exception inside a Socket.BeginReceive() callback - why? 为什么 Visual Studio 没有就 null 引用异常向我发出警告? - Why doesn't Visual Studio warn me about a null reference exception? 发生异常时显示表单C# - Show form when exception occurs C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM