捕获的异常本身为 null !

发布于 2024-10-31 22:03:39 字数 273 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

谁的新欢旧爱 2024-11-07 22:03:39

对于到达这里的任何人,我找到了一个可能的实例(如果只能在调试器中检测到)。 VS2013 Update 4.

损坏:

try
{
    // do something
}
catch (WebException ex) // <- both variables are named 'ex'
{
    Logger.Log("Error while tried to do something. Error: " + ex.Message);
}
catch (Exception ex) // <- this 'ex' is null
{
    Logger.Log("Error while tried to do something. Error: " + ex.Message);
}

解决方案是以不同的方式命名异常变量。

固定的:

try
{
    // do something
}
catch (WebException webEx) // <- all good in the hood
{
    Logger.Log("Error while tried to do something. Error: " + webEx.Message); // <-
}
catch (Exception ex) // <- this 'ex' correctly contains the exception
{
    Logger.Log("Error while tried to do something. Error: " + ex.Message);
}

For anyone ending up here, I've found an instance where this is possible (If only detectable in the debugger). VS2013 Update 4.

Broken:

try
{
    // do something
}
catch (WebException ex) // <- both variables are named 'ex'
{
    Logger.Log("Error while tried to do something. Error: " + ex.Message);
}
catch (Exception ex) // <- this 'ex' is null
{
    Logger.Log("Error while tried to do something. Error: " + ex.Message);
}

The solution is to name your exception variables differently.

Fixed:

try
{
    // do something
}
catch (WebException webEx) // <- all good in the hood
{
    Logger.Log("Error while tried to do something. Error: " + webEx.Message); // <-
}
catch (Exception ex) // <- this 'ex' correctly contains the exception
{
    Logger.Log("Error while tried to do something. Error: " + ex.Message);
}
兮颜 2024-11-07 22:03:39

就我而言,原因是 StackOverflowException。此类异常通常根本不会到达 catch 块,但是这一次,由于某种我不明白的原因,它确实到达了 catch 块,但是异常为null

In my case, the cause was a StackOverflowException. Such exceptions normally don't reach the catch block at all, but this time, for some reason I don't understand, it did reach the catch block, but the exception was null.

半窗疏影 2024-11-07 22:03:39

我刚刚遇到一个问题,有人将 ex.InnerException 传递给一个方法,其中 ex 是根。由于该参数也称为 ex,当我查看最初捕获的异常时,它导致调试器出现一些混乱。这可能是一些粗心重构的结果。

例如:

public void MyMethod(string input)
{
    try {
        Process(input);
    } catch (Exception ex) { // <- (2) Attempting to view ex here would show null
        _logger.log(ex);
        LogInner(ex.InnerException);
    }
}

private void LogInner(Exception ex)
{
    _logger.log(ex); // <- (1) NullReferenceExeption thrown here
    if(ex.InnerException != null)
        LogInner(ex.InnerException);
}

这被重构为:

public void MyMethod(string input)
{
    try {
        Process(input);
    } catch (Exception ex) {
        LogExceptionTree(ex);
    }
}

private void LogExceptionTree(Exception exception)
{
    _logger.log(exception);
    if(exception.InnerException != null)
        LogExceptionTree(exception.InnerException);
}

I just ran into an issue where someone was passing ex.InnerException to a method, where ex was the root. Since the parameter was also called ex 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.:

public void MyMethod(string input)
{
    try {
        Process(input);
    } catch (Exception ex) { // <- (2) Attempting to view ex here would show null
        _logger.log(ex);
        LogInner(ex.InnerException);
    }
}

private void LogInner(Exception ex)
{
    _logger.log(ex); // <- (1) NullReferenceExeption thrown here
    if(ex.InnerException != null)
        LogInner(ex.InnerException);
}

This was refactored as such:

public void MyMethod(string input)
{
    try {
        Process(input);
    } catch (Exception ex) {
        LogExceptionTree(ex);
    }
}

private void LogExceptionTree(Exception exception)
{
    _logger.log(exception);
    if(exception.InnerException != null)
        LogExceptionTree(exception.InnerException);
}
夜无邪 2024-11-07 22:03:39

那不可能发生。

如果抛出 null,您将从 throw 中得到一个 NullReferenceExceptioncatch 块中的异常永远不能为 null

你还有一些null的东西。

That cannot happen.

If you throw null, you'll get a NullReferenceException from the throw; the exception in the catch block can never be null.

You have something else that's null.

相守太难 2024-11-07 22:03:39

当异常是 AggregateException 时,可能会发生这种情况。

This may happen when the Exception is an AggregateException.

神魇的王 2024-11-07 22:03:39

我遇到了同样的问题,原因是:异常是 NullReferenceException,所以你不能使用 ex.Message,你应该尝试以下流程:

try
 {     // do something } 

catch (NullReferenceException)
{
  Logger.Log("Error while tried to do something. Error: Null reference");
}

catch (Exception ex) 
{     
  Logger.Log("Error while tried to do something. Error: " + 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:

try
 {     // do something } 

catch (NullReferenceException)
{
  Logger.Log("Error while tried to do something. Error: Null reference");
}

catch (Exception ex) 
{     
  Logger.Log("Error while tried to do something. Error: " + ex.Message); 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文