简体   繁体   English

单元测试中的C#未捕获异常

[英]C# Uncaught exception in unit test

I'm encountering a very strange issue while debugging a unit test. 我在调试单元测试时遇到了一个非常奇怪的问题。 If I debug the unit test (ctrl+r ctrl+t) I am getting an uncaught exception. 如果我调试单元测试(ctrl + r ctrl + t)我得到一个未捕获的异常。 If I just run the unit test (ctrl+rt) I do not get this exception. 如果我只是运行单元测试(ctrl + rt),我不会得到这个例外。

The uncaught exception is a NHibernate.ByteCode.ProxyFactoryFactoryNotConfiguredException. 未捕获的异常是NHibernate.ByteCode.ProxyFactoryFactoryNotConfiguredException。

Stack trace: 堆栈跟踪:

at NHibernate.Bytecode.AbstractBytecodeProvider.get_ProxyFactoryFactory() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Bytecode\AbstractBytecodeProvider.cs:line 32
at NHibernate.Validator.Util.NHibernateHelper.IsProxyFactoryConfigurated()

I used .Net Reflector to look at the assembly that defines this method (NHibernate.Validator ... it's open source) and here is the method that "throws" the exception: 我用.Net Reflector来查看定义这个方法的程序集(NHibernate.Validator ......它的开源代码),这里是“抛出”异常的方法:

public static bool IsProxyFactoryConfigurated()
{
    try
    {
        IProxyFactoryFactory proxyFactoryFactory = Environment.BytecodeProvider.ProxyFactoryFactory;
        return true;
    }
    catch (ProxyFactoryFactoryNotConfiguredException)
    {
        return false;
    }
}

How can this exception not be caught by that Try Catch block? 如何通过Try Catch块捕获此异常?

It sounds like you're seeing a first chance exception . 听起来你看到了第一次机会异常

Do you have "Break on first-chance exceptions" enabled? 你有“打破第一次机会例外”吗? You should be able to configure it in the Debug->Exceptions menu. 您应该能够在Debug-> Exceptions菜单中对其进行配置。

You probably have Break on All Exceptions set in the debugger, which causes VS to break as soon as an exception is thrown, regardless of whether it's caught. 您可能在调试器中设置了Break on All Exceptions,这会导致VS在抛出异常时立即中断,无论它是否被捕获。
Click Debug, Exceptions. 单击“调试”,“例外”。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM