title: .NET exception caught is unexpectedly null
body: <p><strong>See below for an explanation of what is going on</strong></p>
  
  <p>I have a really weird issue where the exception caught is null.</p>
  
  <p>The code uses MEF and tries hard to report composition errors. Using the debugger I can see the exception being thrown (an <code>InvalidOperationException</code>) but when it is caught by the last catch block in the code below the <code>ex</code> variable is null. This is true both in the debugger and when executing the code normally.</p>
  
  <pre><code>static T ResolveWithErrorHandling&lt;T&gt;() where T : class
  {
      try
      {
          IocContainer.Compose(Settings.Default.IocConfiguration);
          return IocContainer.Resolve&lt;T&gt;();
      }
      catch (ReflectionTypeLoadException ex)
      {
          // ... special error reporting for ReflectionTypeLoadException
      }
      catch (Exception ex)
      {
          // ex is null - that should not be possible!
          // ... general error reporting for other exception types
      }
      return null;
  }
  </code></pre>
  
  <p>The code I have replaced with comments is really simple code to format the error message. Nothing strange going on there.</p>
  
  <p>I have tried to alter the code to discover what effect that might have:</p>
  
  <ul>
  <li>If I remove the first catch block (<code>ReflectionTypeLoadException</code>) the exception caught in the final catch block is no longer null.</li>
  <li>If I catch another exception type in the first catch block the exception caught in the final catch block is no longer null.</li>
  <li>If I add a catch block for <code>InvalidOperationException</code> as the first catch block the exception caught in that block is not null.</li>
  <li>If I add a catch block for <code>InvalidOperationException</code> between the two catch blocks the exception caught in that block is null.</li>
  </ul>
  
  <p>The project uses <a href="http://research.microsoft.com/en-us/projects/contracts/" rel="nofollow noreferrer">Code Contracts</a> and the code generated by the compiler is post-processed to check the contracts. Unfortunately, I havn't figured out a way to get rid of this for testing purposes without performing major surgery on the project.</p>
  
  <p>My current workaround is to not catch <code>ReflectionTypeLoadException</code> and instead branch on the type of <code>ex</code> in the general exception handler.</p>
  
  <p>What could be the explanation for this "impossible" behavior? What is up with <code>ReflectionTypeLoadException</code> catch block?</p>
  
  <hr>
  
  <p>Embarrassingly the exception is not null and it cannot be null per the C# standard 15.9.5.</p>
  
  <p>However, using Code Contracts in a project <a href="https://stackoverflow.com/questions/3191319/code-contracts-for-net-3-5-messes-up-vs10s-debugger">can mess up the display of local variables in the debugger</a> because the IL code generated by the compiler can be rewritten by Code Contracts so the final IL is slightly out of sync with the debug information. In my case the <code>ex</code> variable is displayed as null even it is not. The unfortunate nature of the error reporting taking place right before application termination meant that I believed the error reporting to not be called as a result of <code>ex</code> being null and <code>ex.Message</code> throwing a <code>NullReferenceException</code> inside my catch block. Using the debugger I was able to "verify" that <code>ex</code> was null, except it was actually not null.</p>
  
  <p>My confusion was compounded by the fact that a catch block for <code>ReflectionTypeLoadException</code> seems to affect the debugger display issue.</p>
  
  <p>Thanks to all who responded.</p>