Caught exception is null itself !

后端 未结 5 1186

I have an ASP.NET applications. Everything was fine, but recently I get exceptions that are null themselves:

try
{
    // do something
}
catch (Exception ex)         


        
相关标签:
5条回答
  • 2020-12-10 10:42

    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);
    }
    
    0 讨论(0)
  • 2020-12-10 10:44

    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.

    0 讨论(0)
  • 2020-12-10 10:50

    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); 
    } 
    
    0 讨论(0)
  • 2020-12-10 10:56

    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.

    0 讨论(0)
  • 2020-12-10 11:02

    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);
    }
    
    0 讨论(0)
提交回复
热议问题