捕获的异常本身为 null !
我有一个 ASP.NET 应用程序。一切都很好,但最近我遇到了本身为 null 的异常:
try
{
// do something
}
catch (Exception ex)
{
Logger.Log("Error while tried to do something. Error: " + ex.Message);
}
有时 ex
本身就是 null
!
有什么想法吗?
I have an ASP.NET applications. Everything was fine, but recently I get exceptions that are null themselves:
try
{
// do something
}
catch (Exception ex)
{
Logger.Log("Error while tried to do something. Error: " + ex.Message);
}
Sometimes ex
is null
itself !
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对于到达这里的任何人,我找到了一个可能的实例(如果只能在调试器中检测到)。 VS2013 Update 4.
损坏:
解决方案是以不同的方式命名异常变量。
固定的:
For anyone ending up here, I've found an instance where this is possible (If only detectable in the debugger). VS2013 Update 4.
Broken:
The solution is to name your exception variables differently.
Fixed:
就我而言,原因是 StackOverflowException。此类异常通常根本不会到达
catch
块,但是这一次,由于某种我不明白的原因,它确实到达了catch
块,但是异常为null
。In my case, the cause was a
StackOverflowException
. Such exceptions normally don't reach thecatch
block at all, but this time, for some reason I don't understand, it did reach thecatch
block, but the exception wasnull
.我刚刚遇到一个问题,有人将
ex.InnerException
传递给一个方法,其中ex
是根。由于该参数也称为 ex,当我查看最初捕获的异常时,它导致调试器出现一些混乱。这可能是一些粗心重构的结果。例如:
这被重构为:
I just ran into an issue where someone was passing
ex.InnerException
to a method, whereex
was the root. Since the parameter was also calledex
it led to some confusion in the debugger when I looked at the originally caught exception. This was likely the result of some careless refactoring.e.g.:
This was refactored as such:
那不可能发生。
如果
抛出 null
,您将从throw
中得到一个NullReferenceException
;catch
块中的异常永远不能为null
。你还有一些
null
的东西。That cannot happen.
If you
throw null
, you'll get aNullReferenceException
from thethrow
; the exception in thecatch
block can never benull
.You have something else that's
null
.当异常是 AggregateException 时,可能会发生这种情况。
This may happen when the Exception is an AggregateException.
我遇到了同样的问题,原因是:异常是 NullReferenceException,所以你不能使用 ex.Message,你应该尝试以下流程:
I met the same problem, and the reason is: the exception is a NullReferenceException, so you can not use ex.Message, and you should try the flowing: