title: C# cannot catch StackOverflowException or see where it happened
body: <p>I know this is very vague but this is all I could gather. I hope someone has an insight on how this may be happening. </p>
  
  <p>I have a 64bit program I'm running on Win 7 64bit machine, built in Visual Studio 2012. </p>
  
  <p>It uses a class that is situated in another project (compiled as dll). That class has a buggy iterator that can cause a stack overflow. The operation that calls that buggy iterator is called through a task. </p>
  
  <p>So in my program, code looks like this:</p>
  
  <pre><code>new Task(()=&gt;{
  try
  {
      DoWork(); //&lt;-- buggy iterator is called inside
  }
  catch (Exception ex)
  {
      //Exception reported to user
  }
  }).Start();
  </code></pre>
  
  <p>The code is executed on same data every time, and has no random variables or guid generation involved so should produce same output every time. </p>
  
  <p>Most of the time, it crashes with <em>StackOverflowException occuring in Unknown Module</em> (even though I know for fact where it happens exactly). If that happens, I cannot view the point in code where the overflow occured, and try-catch block does nothing! I cannot continue executing the program (if I run it w/o debugging, it shows the <em>"Program had to be closed lets check for solution online"</em> system window). </p>
  
  <p>Now, sometimes it catches the exception (the correct place in code that caused the exception is showed). This is random but seems to occur more if I break the task more often (like randomly insert breakpoints into the task during its work). </p>
  
  <p>However, I still cannot continue the application. Even though the exception happens inside a try-catch block that is supposed to catch StackOverflowException (it is supposed to catch it because StackOverflowException is included in Exception - C# wont let you catch both and tells you explicitly that SOE is already included in E). </p>
  
  <p>I tried making a small program that makes deliberate stack overflow inside a try-catch block and it also showed the <em>"Program had to be closed lets check for solution online"</em> system window for me instead of handling that correctly. Here's the code:</p>
  
  <pre><code>    private bool Overflow()
      {
          return Overflow();
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
          try { Overflow();}
          catch (StackOverflowException ex) { }
      }
  </code></pre>
  
  <p>Therefore, I have several questions:</p>
  
  <p><strong>Question 1:</strong></p>
  
  <p>Is it possible to catch StackOverflowExceptions ever? How do I do it?</p>
  
  <p><strong>Question 2:</strong></p>
  
  <p>When stack overflow happens inside your dll (other project in same solution), what can make VS 2012 debugger think it's "Unknown module"? </p>
  
  <p><strong>Question 3:</strong></p>
  
  <p>Why doing some wierd magic dancing  with tribal musical instruments makes it do that less often? (by that I mean randomly breaking program in-progress)</p>
  
  <p>Just overall, what is going on?</p>
  
  <p>Thanks in advance</p>