如何才能钓到例外是null(不NullReferenceException异常)? [英] How can catched exception be null (not NullReferenceException)?

查看:211
本文介绍了如何才能钓到例外是null(不NullReferenceException异常)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个相当奇怪的小问题。

I have run into a rather weird little problem.

在以下code我无法理解电子可以是;

In the following code I can not understand how e can be null;

try
{
    //Some Code here
}
catch (Exception e)
{
    //Here e is null
}

据我所知,掷空将被转换为抛出新的NullReferenceException()

这个问题似乎涉及到多线程,去掉另一个线程也似乎解决它。或者至少,我只看到了这一点,当上述code是运行在一个新的线程。整个程序使用多个线程,是一个有点复杂。

The problem seems to be related to multithreading, as removing another thread also seems to fix it. Or at least I have only seen this when the above code is run in a new thread. The whole program uses many threads and is a bit complex.

反正我的问题是,如何能够电子为空? - 希望这个问题的答案可以帮助找到这个问题的根源

Anyway my question is, how can e be null? - Hopefully the answer to that can help find the source of this problem.

修改 我发现了它,因为它引起了catch语句一个NullReferenceException,并使用调试器我看到了同样的事情。

Edit I discovered it since it caused a NullReferenceException in the catch statement, and using the debugger I see the same thing.

编辑2 打开VisualStudio中,第二天又试了一次,没有code的变化和现在同样的口头禅是叫,但这次e为不为空。看来这是一个VS故障。

Edit 2 Open VisualStudio the next day tried again, no code changes and now the same catch phrase is "called" but this time e is not null. It appears it was a VS glitch.

推荐答案

你是如何确定e是实际上空?我已经尝试了几件样品,并通过对异常的CLI规范读取和它似乎并没有允许有例外值为空。此外,如果它是空,它不会有一个类型,因此将无法满足筛选条件是异常的类型。

How are you determining that e is in fact null? I've tried a few samples and read through the CLI spec on exceptions and it does not seem to allow for a exception value being null. Additionally if it was null, it would not have a type and hence wouldn't be able to meet the filter criteria for being of type exception.

您使用调试器来验证这个值?如果是这样,尝试切换到内联断言。

Are you using the debugger to verify this value? If so, try switching it to an inline assert.

这篇关于如何才能钓到例外是null(不NullReferenceException异常)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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