Caught exception is null itself !
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);
}
Comments
-
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
isnull
itself !Any idea?
-
Andrew Barber about 13 years+1 Stack Overflow questions make me test the darndest things!
-
CodesInChaos about 13 yearsI wouldn't be surprised if it's possible to throw
null
somehow. I think it's already possible to throw classes not derived fromException
. But I'd be surprised ifcatch(Exception ex)
would catch such anull
exception. -
Andrew Barber about 13 yearsNope. It's just like SLaks said: If you throw a null reference, you get a
NullReferenceException
. I just tried myself, too. -
Xaqron about 13 yearsI don't
throw
the exception. Also I put aBreak Point
there andex
was null. Maybe it's not aCLR
issue and it's theVisual Studio 2010
. -
Xaqron about 13 yearsRecently I used custom exceptions in
web.config
underhttpErrors>
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 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 about 13 yearsYou are wrong. A
NullReferenceException
will not have a nullMessage
. -
Pavel Vyazankin over 10 yearsIt's about Java. But you cat found it interesting adarshr.com/papers/npe
-
BatteryBackupUnit about 9 yearsI 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 thetry
clause the issue does not occur. -
BatteryBackupUnit about 9 yearsI've reported the issue on MS Connect here. You're welcome to upvote it.
-
Manfred about 8 yearsSame problem and fix with Visual Studio 2013 Update 5.
-
Wali almost 8 yearsThis is the right answer... i have suffered from it. The issue still exists in VS2015.
-
Kohanz over 6 yearsThis definitely CAN happen. Have experienced it myself.
-
Amitava Karan over 6 yearsfor me, there is only one catch block. still, I got the issue. not sure what's wrong with the code?
-
lucuma almost 6 yearsI ran into the same issue. The outer exception was named
Exception ex
and the inner was too and returning null. -
Mel almost 5 yearsWe 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 over 4 yearsThe issue also currently could be observed in Jetbrainbs Rider IDE.
-
BBlake about 4 yearsI 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 over 3 yearsThis 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 about 3 yearsstill in 16.9.2
-
Marco Leite almost 2 yearsRunning VS 2022 Version 17.0.1 and this is still an issue :facepalm: