Caught exception is null itself !

17,263

Solution 1

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);
}

Solution 2

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.

Solution 3

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);
}
Share:
17,263
Xaqron
Author by

Xaqron

Physician

Updated on June 22, 2022

Comments

  • Xaqron
    Xaqron almost 2 years

    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?

  • Andrew Barber
    Andrew Barber about 13 years
    +1 Stack Overflow questions make me test the darndest things!
  • CodesInChaos
    CodesInChaos about 13 years
    I wouldn't be surprised if it's possible to throw null somehow. I think it's already possible to throw classes not derived from Exception. But I'd be surprised if catch(Exception ex) would catch such a null exception.
  • Andrew Barber
    Andrew Barber about 13 years
    Nope. It's just like SLaks said: If you throw a null reference, you get a NullReferenceException. I just tried myself, too.
  • Xaqron
    Xaqron about 13 years
    I don't throw the exception. Also I put a Break Point there and ex was null. Maybe it's not a CLR issue and it's the Visual Studio 2010.
  • Xaqron
    Xaqron about 13 years
    Recently I used custom exceptions in web.config under httpErrors> and put a <clear/> as the first node and then <error statusCode="400" responseMode="Redirect" path="Error.aspx?Code=400"/> but I commented it after this problem.
  • Andrew Barber
    Andrew Barber about 13 years
    @Xaqron - We are not saying you are throwing a null exception. We're saying it's not possible to do so. Also, that area of web.config has nothing at all to do with your problem, in all likelihood.
  • SLaks
    SLaks about 13 years
    You are wrong. A NullReferenceException will not have a null Message.
  • Pavel Vyazankin
    Pavel Vyazankin over 10 years
    It's about Java. But you cat found it interesting adarshr.com/papers/npe
  • BatteryBackupUnit
    BatteryBackupUnit about 9 years
    I experience the same issue with VS2013 Update 4. have you already reported this bug to microsoft? I've tried to, but it seems hard to create a repro. When i add a simple throw new InvalidOperationException() into the try clause the issue does not occur.
  • BatteryBackupUnit
    BatteryBackupUnit about 9 years
    I've reported the issue on MS Connect here. You're welcome to upvote it.
  • Manfred
    Manfred about 8 years
    Same problem and fix with Visual Studio 2013 Update 5.
  • Wali
    Wali almost 8 years
    This is the right answer... i have suffered from it. The issue still exists in VS2015.
  • Kohanz
    Kohanz over 6 years
    This definitely CAN happen. Have experienced it myself.
  • Amitava Karan
    Amitava Karan over 6 years
    for me, there is only one catch block. still, I got the issue. not sure what's wrong with the code?
  • lucuma
    lucuma almost 6 years
    I ran into the same issue. The outer exception was named Exception ex and the inner was too and returning null.
  • Mel
    Mel almost 5 years
    We just saw this not ten minutes ago. Renaming the second catch variable made it go away. This is just... wow. How have we never run into this before. I name ALL my exception catch variables "ex", and I've never seen this before today, but we can absolutely 100% reproduce this.
  • dbardakov
    dbardakov over 4 years
    The issue also currently could be observed in Jetbrainbs Rider IDE.
  • BBlake
    BBlake about 4 years
    I have just run into something similar in VS 2019. I have an ASP.NET web app where a controller action has two separate try/catch blocks. When the first block doesn't catch the error, but the second block does, and both define the catch as catch(Exception ex), then ex is always null in the second block. Renaming the second block's ex variable solved the problem.
  • Bas
    Bas over 3 years
    This still happens in VS 2019 16.6.3. I have a WebSocketException catch block followed by a System.Exception catch block, and if they are both named "ex", the System.Exception catch will always be null.
  • Wouter
    Wouter about 3 years
    still in 16.9.2
  • Marco Leite
    Marco Leite almost 2 years
    Running VS 2022 Version 17.0.1 and this is still an issue :facepalm: