title: debugging async C# in visual studio; breaking on exceptions as expected
body: <p>I'm struggling to figure out how to use visual studio to debug async code sanely, since it's not breaking where I want it to.  For example, in this code:</p>
  
  <pre><code>using System;
  using System.Threading.Tasks;
  
  namespace TestNS{
      class Program {
          static void Main(string[] args) {
              Foo().Wait(); // sadly, it breaks here
          }
  
          static async Task Foo() {
              var foo = 42;
              if (foo == 42) {
                  throw new Exception(); // want it to break here
              }
          }
      }
  }
  </code></pre>
  
  <p>How can I get Visual Studio to automatically break where the exception is thrown and not at the top of my program?  Or equivalently, without restarting the process, can I jump to that context and see the values of those locals?</p>
  
  <p>I understand the root problem is that the exceptions are technically being handled by the behind the scenes async code, so VS doesn't see them as being unhandled.  Surely no one else likes this behavior though, and there's a way to tell VS that exceptions that bubble up to an async call aren't really caught... I've tried turning "just my code", "break when this exception is thrown", etc., on and off, but nothing helps.</p>
  
  <p>I am not looking for a solution that includes editing my code or manually adding breakpoints.  Similarly, handing certain exceptions in special ways is also not a real solution.  Not all the exceptions I want to break on are from my own code; often it will come from a missing key in a dictionary, or something like that. 
   Unexpected exceptions happen all the time when developing code, and I want to see that program state easily without manually placing breakpoints and/or try/catch code, <em>rerunning the program</em>, manually advancing the program until I happen to get to the function.  And, that's all under the assumption I can even get it to trigger again, when I don't know anything about why it happened in the first place.  And then do that for every exception that ever might occur during development.</p>
  
  <p>On top of all that, if you're looking at the <code>AggregateException</code> that does come back, there isn't even a nice way to figure out where that exception comes from.  You've got to "view details", then expand the exception, expand the inner exception, hover over the stack trace, <em>remember the file and line number</em>, close the details, go to the right place manually, and now put manual breakpoints and go debug manually.  Why can't the system use this stack trace to do the right thing?</p>
  
  <p>I'm putting all these details in because I have read 20 other SO posts about similar topics.  They don't put all these details in, and people seem to go off happily, so I'm confused what I'm doing wrong.  None of this happens when developing non-async code.  Are all of the steps I'm doing above necessary?  What is the best practice to make writing async code as pleasant as writing non-async code in terms of examining exception context?</p>