title: Caught OutOfMemoryException makes debugging difficult
body: <p>When I debug my program, and try to do certain things in the immediate window, it sometimes shows an error message in the immediate window saying: </p>
  
  <blockquote>
    <p>The function evaluation was disabled because of an out of memory
    exception.</p>
  </blockquote>
  
  <p>It also shows that when viewing the properties of object by hovering over them.</p>
  
  <p>After trying to find the cause of the problem, I narrowed it down to this small code sample: </p>
  
  <pre><code>using System;
  using System.Text.RegularExpressions;
  
  namespace ConsoleApplication2
  {
      class Program
      {
          static void Main(string[] args)
          {
              try
              {
                  //outofmemoryexception can be thrown by Image.FromFile("path/that/does/not/exist.png")
                  //if the path points to a file that is not an image
                  throw new OutOfMemoryException();
              }
              catch (OutOfMemoryException ex)
              {
                  //caught the exception
                  //so no problem, right?
              }
  
              //Random object to use in immediate window
              Random rand = new Random();
  
              //Also, try hovering over this regex and take a look at its properties.
              var test = new Regex("");
  
              //put a breakpoint here (at the next closing curly brace) and try calling rand.Next() in the immediate window
          }
      }
  }
  </code></pre>
  
  <p>It seems that the debugger freaks out when an OutOfMemoryException occurs, even when it's caught... </p>
  
  <p>I can imagine that no one ever thought it would be possible to debug a program that has had an OutOfMemoryException. But sadly enough Image.FromFile throws that error when the file is not an image...</p>
  
  <p><strong>Questions:</strong></p>
  
  <ol>
  <li>Does the above code sample give problems to anyone else?</li>
  <li>Can someone clarify this a bit? Why does this happen exactly?</li>
  <li>And lastly, how can I prevent this?</li>
  </ol>