How can an exception be thrown null (and not a NullReferenceException)?

I got a rather strange little problem.

In the following code, I cannot figure out how e can be null ;

 try { //Some Code here } catch (Exception e) { //Here e is null } 

As far as I know, throw null will be converted to throw new NullReferenceException() .

The problem seems to be related to the multi-threaded process, since deleting another thread also seems to fix it. Or at least I saw it only when the above code runs in a new thread. The whole program uses a lot of threads and is a bit complicated.

Anyway, my question is: how can e be null? β€œI hope the answer to this helps find the source of this problem.”

Edit I found this because it threw a NullReferenceException in the catch statement, and with the debugger I can see the same thing.

Edit 2 Having opened VisualStudio the next day, I tried again, the code has not changed, and now the same catch phrase is β€œcalled”, but this time e is not null. It seems to have been a VS crash.

+5
multithreading c # exception exception-handling
Dec 03 '09 at 17:22
source share
4 answers

How do you determine that e is actually null? I tried several samples and read the CLI specification for exceptions and did not seem to allow the exception value to be zero. In addition, if it was null, it would not have a type and, therefore, would not meet the filter criteria to exclude the type.

Do you use a debugger to check this value? If so, try switching it to the built-in assert.

+7
Dec 03 '09 at 17:35
source share

Are you sure you were out of line Exception?

 try { //Some Code here } catch (Exception e) { int i = 0; // breakpoint here } 

I only ask about it because I have never seen such behavior, and I know that if you stop Exception e, e seems to be null. On the next line, it becomes non-zero.

+2
Dec 03 '09 at 18:09
source share

It is possible that the exception thrown is not a CLS Compliant, which really shouldn't be fun using Try / Catch with a filter.

You can only catch CLS Compliant with try {} catch {} with no argument "

+1
Dec 03 '09 at 18:07
source share

I have the same situation. This was an Eclipse debugger error. An Eclipse exception is sufficient - a run-time exception becomes normal, not null.

0
Jan 18 '12 at 9:23
source share



All Articles