Can I let Visual Studio dwell on unhandled exceptions inside the task code?

Visual Studio has a specific function that greatly facilitates debugging unhandled exceptions: it stops at the violating line of code and displays an exception.

It seems that the Task class is designed in such a way that this function is always suppressed: it catches each exception and then raises another exception when the Wait task is modified or completed.

I know that I can stop it with exceptions with a first chance, but it doesnโ€™t always help: imagine that before the processed case there are several processed exceptions of the same type. In this case, VS will dwell on each non-problematic exception in addition to what really causes the problem.

Another alternative is even less acceptable: just looking at the InnerException stack InnerException : this means that although I know which line threw an exception, I cannot access any of its local state, as if the program was actually stopped there .

Can I somehow get the best of both worlds using the Task class, but not having to work with a set of debugging features for excluded exceptions?


Bonus question: does this mean that excluding the link in the await block will not cause Visual Studio to stop there, but instead stop somewhere else?

+6
source share
1 answer

The Task type carries all exceptions to an AggregateException . However, if you use async / await functionality, then when you await a Task , an internal exception is thrown and thrown again, preserving the original stack trace.

VS11 will have better async debugging support, but I don't think you can go as far as you hope. Task is all about concurrent and asynchronous code, and therefore I donโ€™t think it will ever work.

Consider, for example, if you have a Task running in a thread in a thread pool that you are going to await . You can await in the try block to catch an exception from the Task ... or you can await outside the try block, leaving the Task exception unhandled.

The point with this example is that when an exception is thrown, the debugger does not know if the exception will be unhandled. With synchronous code, as soon as an exception is thrown, the stack is checked - and if it is unprocessed, the debugger knows that it is unprocessed and can immediately take special actions (before the stack is even unwound).

So, I do not think that you can do what you want. You can get pretty close to IntelliTrace though (only in VS Ultimate).

+3
source

Source: https://habr.com/ru/post/906920/


All Articles