NullReferenceException handler throws NullReferenceException!

  • Thread starter Thread starter Zytan
  • Start date Start date
Z

Zytan

I just got the strangest error. I have exception code that catches a
null reference exception:
catch (NullReferenceException ex) { ... }
The handling code does nothing but print out data from within the
object, ex. So, nothing is changed. And I've found that the
exception object, ex, ITSELF is null!

So, by accessing it to print its innards, it throws another
NullReferenceException (which is, of course, unhandled)! Printing
ex.Message actually works, it reports "Object reference not set to an
instance of an object." But, accessing ex.InnerException is what
throws the null reference, since ex is null.

Strange. I'll see if I can replicate this, and nail it down.

Zytan
 
Strange. I'll see if I can replicate this, and nail it down.

I think the null reference was from ex.InnerException being null. And
I was trying to print ex.InnerException.Message. However, what threw
me off was that viewing ex in the debugger shows:

ex {"Object reference not set to an instance of an object."}
System.NullReferenceException

This made me believe ex was null. (Of course, it would show "null" if
it was null.) But, if the object referenece in not set to an instance
of an object, then WTH is it set to? In the debugger, under ex, it
shows no methods, just "base". Under that, "base" again. Finally,
under that, it shows some methods.

Zytan
 
But, if the object referenece in not set to an instance
of an object, then WTH is it set to?

LOL! It's telling me that the exception IS that an object was
referenced that was null (not set to an instance of an object).

Zytan
 
Zytan said:
I just got the strangest error. I have exception code that catches a
null reference exception:
catch (NullReferenceException ex) { ... }
The handling code does nothing but print out data from within the
object, ex. So, nothing is changed. And I've found that the
exception object, ex, ITSELF is null!

So, by accessing it to print its innards, it throws another
NullReferenceException (which is, of course, unhandled)! Printing
ex.Message actually works, it reports "Object reference not set to an
instance of an object." But, accessing ex.InnerException is what
throws the null reference, since ex is null.

Strange. I'll see if I can replicate this, and nail it down.

ex isn't null, InnerException is.
 
Zytan said:
I think the null reference was from ex.InnerException being null. And
I was trying to print ex.InnerException.Message. However, what threw
me off was that viewing ex in the debugger shows:

ex {"Object reference not set to an instance of an object."}
System.NullReferenceException

This made me believe ex was null. (Of course, it would show "null" if
it was null.) But, if the object referenece in not set to an instance
of an object, then WTH is it set to? In the debugger, under ex, it
shows no methods, just "base". Under that, "base" again. Finally,
under that, it shows some methods.

The debugger shows ex.ToString(), which shows you ex.Message. ex is indeed
an instance of NullReferenceException, with InnerException=null and
Message="Object reference not set to an instance of an object."
 
The debugger shows ex.ToString(), which shows you ex.Message. ex is indeed
an instance of NullReferenceException, with InnerException=null and
Message="Object reference not set to an instance of an object."

Yup, thanks Ben, I figured it out somewhat myself. But, yeah, it's
nice to know that ToString() returns Message.

Zytan
 
Zytan said:
Yup, thanks Ben, I figured it out somewhat myself. But, yeah, it's
nice to know that ToString() returns Message.

Peeking at mscorlib with the insanely useful .NET Reflector
(http://www.aisto.com/roeder/dotnet/), we can see exactly how
Exception.ToString() works:

public override string ToString()
{
string text2;
string message = this.Message;
if (this._className == null)
{
this._className = this.GetClassName();
}
if ((message == null) || (message.Length <= 0))
{
text2 = this._className;
}
else
{
text2 = this._className + ": " + message;
}
if (this._innerException != null)
{
text2 = text2 + " ---> " + this._innerException.ToString() +
Environment.NewLine + " " +
Environment.GetResourceString("Exception_EndOfInnerExceptionStack");
}
if (this.StackTrace != null)
{
text2 = text2 + Environment.NewLine + this.StackTrace;
}
return text2;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top