【问题标题】:Caught exception is null itself !捕获的异常本身为 null !
【发布时间】:2021-02-04 00:20:25
【问题描述】:

我有一个 ASP.NET 应用程序。一切都很好,但最近我得到了自己为空的异常:

try
{
    // do something
}
catch (Exception ex)
{
    Logger.Log("Error while tried to do something. Error: " + ex.Message);
}

有时ex 本身就是null

有什么想法吗?

【问题讨论】:

  • 可能不是异常为空,而是编译器可能正在优化其余部分代码,导致您的异常出现在其他地方。您可以粘贴任何实际代码吗?
  • 另外,您是否已将问题范围缩小到当您收到 null ex(或 null ex.Message)时发生的一致情况?
  • exnull。我放了一个Break Point,我确定它是ex
  • 这完全不可能发生,除非你事先写ex = null;
  • 你把断点放在哪里了?如果在 'catch' 行中,然后将鼠标指针悬停,您实际上可能会得到空值。您需要在 catch 语句中运行代码以将异常引用到 ex。

标签: c# asp.net exception null


【解决方案1】:

对于任何在这里结束的人,我已经找到了一个可能的实例(如果只能在调试器中检测到)。 VS2013 更新 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);
}

【讨论】:

  • 我在 VS2013 更新 4 中遇到了同样的问题。您是否已经向微软报告了这个错误?我试过了,但似乎很难创建一个复制品。当我在try 子句中添加一个简单的throw new InvalidOperationException() 时,问题不会发生。
  • 我已经在 MS Connect here 上报告了这个问题。欢迎您投票。
  • 同样的问题并用 Visual Studio 2013 Update 5 修复。
  • 这是正确的答案...我已经受苦了。 VS2015 中问题依然存在。
  • 对我来说,只有一个 catch 块。仍然,我得到了这个问题。不确定代码有什么问题?
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    我刚刚遇到了一个问题,有人将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);
    }
    

    【讨论】:

    • 我遇到了同样的问题。外部异常命名为 Exception ex,内部异常也命名为 null。
    【解决方案4】:

    这不可能发生。

    如果您throw null,您将从throw 获得NullReferenceExceptioncatch 块中的异常永远不能是 null

    你还有别的东西是null

    【讨论】:

    • +1 堆栈溢出问题让我测试最糟糕的事情!
    • 如果可以抛出null不知何故,我不会感到惊讶。我认为已经可以抛出不是从Exception 派生的类。但是如果catch(Exception ex) 会捕获这样的null 异常,我会感到惊讶。
    • 不。就像 SLaks 说的:如果你抛出一个空引用,你会得到一个NullReferenceException。我自己也试过了。
    • 我不throw 例外。我还在那里放了一个Break Point,而ex 为空。也许这不是CLR 问题,而是Visual Studio 2010
    • 这绝对会发生。亲身经历过。
    【解决方案5】:

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

    【讨论】:

      【解决方案6】:

      我也遇到了同样的问题,原因是:异常是NullReferenceException,所以不能使用ex.Message,应该试试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); 
      } 
      

      【讨论】:

      • 你错了。 NullReferenceException 不会有空的 Message
      猜你喜欢
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-24
      • 2023-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多