Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

debugging async C# in visual studio; breaking on exceptions as expected

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:

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
            }
        }
    }
}

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?

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.

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, rerunning the program, 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.

On top of all that, if you're looking at the AggregateException 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, remember the file and line number, 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?

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?

like image 260
Chucky Ellison Avatar asked Mar 22 '19 23:03

Chucky Ellison


1 Answers

you need to activate Common Language Runtime exception using CTRL + ALT + E as in the picture below .

enter image description here

like image 106
sayah imad Avatar answered Oct 04 '22 04:10

sayah imad