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?
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:
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.
That cannot happen.
If you
throw null
, you'll get aNullReferenceException
from thethrow
; the exception in thecatch
block can never benull
.You have something else that's
null
.I just ran into an issue where someone was passing
ex.InnerException
to a method, whereex
was the root. Since the parameter was also calledex
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.:
This was refactored as such:
For anyone ending up here, I've found an instance where this is possible (If only detectable in the debugger). VS2013 Update 4.
Broken:
The solution is to name your exception variables differently.
Fixed: