46

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?

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
Xaqron
  • 29,931
  • 42
  • 140
  • 205
  • 3
    Chances are it's not the exception that's null but the compiler might be optimising out some of the rest of the code which causes your exception to appear elsewhere. Can you paste any of your actual code? – Matthew Steeples Apr 12 '11 at 11:30
  • Also, have you narrowed down the issue to something consistent that is happening when you get the null ex (or null ex.Message)? – DashTechnical Apr 12 '11 at 11:31
  • `ex` is `null`. I put there a `Break Point` and I'm sure it is `ex`. – Xaqron Apr 12 '11 at 11:46
  • It is completely impossible for that to happen, unless you write `ex = null;` beforehand. – SLaks Apr 12 '11 at 13:11
  • 7
    Where did you put the breakpoint? if in 'catch' line then hover the mouse pointer you might actually get null. You need to run the codes inside the catch statement to reference the exception to ex. – hallie Apr 14 '11 at 08:04
  • Did you put the breakpoint on the catch line? If you did then ex will be null since it hasn't actually run that piece of code yet. Put the breakpoint on the Logger line then look at the value of ex. – Phill Apr 14 '11 at 08:06
  • @Mathhew: You right. It was the compiler. I should ask it in another question why sometimes (without any change) debugger says that a module has optimized. – Xaqron Apr 14 '11 at 11:50
  • How did you address it? I just encountered the same problem. Here is a screen shot. http://www.flickr.com/photos/7200684@N02/7521732858/in/photostream – Joel Jul 07 '12 at 17:54
  • Hope my answer to this quest might help you. http://stackoverflow.com/a/18508670/731894 – CrazyGreenHand Aug 29 '13 at 10:48

6 Answers6

64

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);
}
Community
  • 1
  • 1
Ben Cull
  • 9,434
  • 7
  • 43
  • 38
  • 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 Feb 05 '15 at 08:09
  • I've reported the issue on MS Connect [here](https://connect.microsoft.com/VisualStudio/Feedback/Details/1116170). You're welcome to upvote it. – BatteryBackupUnit Feb 05 '15 at 12:36
  • Same problem and fix with Visual Studio 2013 Update 5. – Manfred Feb 22 '16 at 05:29
  • 2
    This is the right answer... i have suffered from it. The issue still exists in VS2015. – Wali May 25 '16 at 16:53
  • for me, there is only one catch block. still, I got the issue. not sure what's wrong with the code? – Amitava Karan Oct 31 '17 at 13:38
  • The issue also currently could be observed in Jetbrainbs Rider IDE. – dbardakov Jan 10 '20 at 10:05
  • 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. – BBlake Feb 26 '20 at 16:28
  • 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. – Bas Jul 25 '20 at 17:19
  • still in 16.9.2 – Wouter Apr 14 '21 at 17:27
  • Running VS 2022 Version 17.0.1 and this is still an issue :facepalm: – Marco Leite Apr 26 '22 at 11:15
  • Still an issue here..... – Chris Nevill Feb 23 '23 at 15:13
6

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.

Serj Sagan
  • 28,927
  • 17
  • 154
  • 183
William Jockusch
  • 26,513
  • 49
  • 182
  • 323
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);
}
Greg
  • 404
  • 4
  • 15
  • I ran into the same issue. The outer exception was named `Exception ex` and the inner was too and returning null. – lucuma May 10 '18 at 22:48
0

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    +1 Stack Overflow questions make me test the darndest things! – Andrew Barber Apr 12 '11 at 11:34
  • 1
    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. – CodesInChaos Apr 12 '11 at 11:40
  • Nope. It's just like SLaks said: If you throw a null reference, you get a `NullReferenceException`. I just tried myself, too. – Andrew Barber Apr 12 '11 at 11:44
  • 5
    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 Apr 12 '11 at 11:49
  • Recently I used custom exceptions in `web.config` under `httpErrors>` and put a `` as the first node and then `` but I commented it after this problem. – Xaqron Apr 12 '11 at 11:52
  • @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. – Andrew Barber Apr 12 '11 at 12:00
  • It's about Java. But you cat found it interesting http://www.adarshr.com/papers/npe – Pavel Vyazankin Sep 03 '13 at 20:06
  • 5
    This definitely CAN happen. Have experienced it myself. – Kohanz Oct 25 '17 at 20:01
  • 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. – Mel Jun 07 '19 at 16:57
0

This may happen when the Exception is an AggregateException.

Thibaut
  • 41
  • 3
-2

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); 
} 
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964