Bizarre Exception null value

  • Thread starter Thread starter Joannes Vermorel
  • Start date Start date
J

Joannes Vermorel

Looking at the debugger of VS .Net, I have discovered some bizarre behavior
of the Exception type. When I do <c>Exception e = null;<c/> I notice that
'e' is not actually null (<c>e == null<c/> return false) but is not defined
either (<c>e.GetType()<c/> throws an other exception). In the .Net doc, it
seems that Exception is a CLS compliant type. So why this very bizarre
behavior ? And next question, how do I check that an Exception has a null
value?

Joannès
 
I think you are just seeing some debugger weirdness:

static void Main(string[] args)
{
Exception e = null;
if( e == null )
Console.WriteLine("null");
else
Console.WriteLine("weird");
}

prints out "null"

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Looking at the debugger of VS .Net, I have discovered some bizarre behavior
of the Exception type. When I do <c>Exception e = null;<c/> I notice that
'e' is not actually null (<c>e == null<c/> return false) but is not defined
either (<c>e.GetType()<c/> throws an other exception). In the .Net doc, it
seems that Exception is a CLS compliant type. So why this very bizarre
behavior ? And next question, how do I check that an Exception has a null
value?

Joann?s
 
Joannes Vermorel said:
Looking at the debugger of VS .Net, I have discovered some bizarre behavior
of the Exception type. When I do <c>Exception e = null;<c/> I notice that
'e' is not actually null (<c>e == null<c/> return false)

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
but is not defined
either (<c>e.GetType()<c/> throws an other exception).

e.GetType() *would* throw an exception - there's nothing bizarre about
that. It's trying to dereference null, which throws an exception.
In the .Net doc, it seems that Exception is a CLS compliant type. So
why this very bizarre behavior ?
And next question, how do I check that an Exception has a null value?

if (e==null) like any other reference type.
 
I think you are just seeing some debugger weirdness:

I just checked your code. Indeed, it was just some debugger weirdness.
Thanks for the help.

Joannès
 
Back
Top