.NET exception caught is unexpectedly null

后端 未结 6 1652
囚心锁ツ
囚心锁ツ 2021-02-05 01:16

See below for an explanation of what is going on

I have a really weird issue where the exception caught is null.

The code uses MEF and tries har

相关标签:
6条回答
  • 2021-02-05 01:39

    Just ran into this same problem. I finally found out that I catched different exceptions with the same name, like you did:

    catch (ReflectionTypeLoadException ex)
    {
        // ... 
    }
    catch (Exception ex)
    {
        // ex is not null!
        // ...
    }
    

    Both are named 'ex'. Changing one of both names solved this problem for me, like:

    catch (ReflectionTypeLoadException reflectionEx)
    {
        // ... 
    }
    catch (Exception ex)
    {
        // ex is null - that should not be possible!
        // ...
    }
    
    0 讨论(0)
  • 2021-02-05 01:47

    You should check if at some point, the IocContainer catches an Exception ex throws ex.InnerException without checking if it is null.

    C# happily accepts throw null, and ends up in catch (Exception).

    0 讨论(0)
  • 2021-02-05 01:47

    I ran into the same problem. The exception was null when viewed in the debugger even though the correct type of exception - UpdateException - was being caught. I could view the exception by opening the Exception Assistant.

    As soon as I turned off "Perform Runtime Contract Checking" caught exceptions where no longer null. I have been actively using code contracts for going on a year now and had not seen this problem before I starting working with EF 4.1 in this particular project recently - but I do not know if EF was a controlling variable in regards to caught exceptions being null.

    0 讨论(0)
  • 2021-02-05 01:51

    I have got the same situation, too. It happened to be a bug of Eclipse debugger. (Really, this situation can be only the result of some debugger's bug. )

    Eclipse restart was enough - runtime exception becomes normal, not null. Other debuggers could be not so kind.

    0 讨论(0)
  • 2021-02-05 01:53

    The exception is in fact not null, it's a problem with the debugger. Code contracts (ccrewrite) changes IL opcodes and that perturbates the debugger, because leave.s opcodes are transformed into leave opcodes. The two opcodes have different sizes and instruction adresses change, that's why the debugger is lost when exception names are the same.

    You can use $exception in the debugger to workaround the issue.

    0 讨论(0)
  • 2021-02-05 02:01

    I ran in the same problem. In my case renaming the exception variable (e.g. ex => ex1) allowed to me to catch any exception...

    0 讨论(0)
提交回复
热议问题