Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async: How to break on exact line of code that has thrown the exception?

Say I have the following code:

async void buttonClick(object sender, RoutedEventArgs e)
{
    await nested1();
}

async Task nested1()
{
    await nested2();
}

async Task nested2()
{
    await nested3();
}

async Task nested3()
{
    await Task.Delay(1000);
    throw new Exception("Die");  // <-- I want Visual Studio to break on this line
}

How can I make Visual Studio break on the indicated line only when the exception is unhandled?


Normally when the exception is thrown, Visual Studio will break here in App.g.i.cs:

enter image description here

If I then enable "Just my code" in the debugger options, it will break here instead:

enter image description here

Getting close. If I then check System.Exception under the "Thrown" column in the Exceptions window (Ctrl+Alt+E), it will break exactly where I want:

enter image description here

but now Visual Studio will break on all thrown exceptions, not just unhandled ones. Is there any way to get Visual Studio to break at the most logical line of code only for unhandled exceptions, just like it would in regular non-async code? If this behavior is not possible, then:

  • Why is it not possible?
  • How can I identify the line of code that has thrown the exception, whether that be a throw statement of my own code, or a framework method call that has thrown an exception?

I've used 3 nested functions here in this example to emphasize that my code may be complex with many branches and nested method calls, and identifying the problematic line of code is difficult if Visual Studio doesn't break on the innermost line of code.

like image 880
Decade Moon Avatar asked Oct 07 '14 08:10

Decade Moon


People also ask

How do you break an exception thrown?

Tell the debugger to break when an exception is thrown In the Exception Settings window (Debug > Windows > Exception Settings), expand the node for a category of exceptions, such as Common Language Runtime Exceptions. Then select the check box for a specific exception within that category, such as System.

What happens if an exception is thrown within an asynchronous method?

Async void methods have different error-handling semantics. When an exception is thrown out of an async Task or async Task method, that exception is captured and placed on the Task object.

How do I get async exception?

To catch an exception that an async task throws, place the await expression in a try block, and catch the exception in a catch block. Uncomment the throw new Exception line in the example to demonstrate exception handling. The task's IsFaulted property is set to True , the task's Exception.

Can async return void?

In short, if your async method is an event handler or a callback, it's ok to return void .


2 Answers

I think I may have discovered the answer to my question.

First, I uncheck System.Exception under the "Thrown" column in the exceptions window (Ctrl+Alt+E). I do this because I don't want Visual Studio to break on all exceptions, only unhandled ones.

Second, I keep "Just my code" enabled in the debugger options.

Now, when the debugger breaks, it will break here:

enter image description here

as per the screenshot in my question. But if I inspect the call stack:

enter image description here

I can actually see where the exception originated from (in nested3(), line 46). If I click this entry in the call stack window, Visual Studio will jump to the correct line, and the "Locals" window will even show me the values of local variables in that frame. Cool! I think this is what I wanted.

like image 120
Decade Moon Avatar answered Oct 24 '22 16:10

Decade Moon


It's not possible.

Why is it not possible?

Because it's not done yet. async is still getting better tooling support with every release, and I do expect this behavior to be added sooner or later.

How can I identify the line of code that has thrown the exception, whether that be a throw statement of my own code, or a framework method call that has thrown an exception?

Visual Studio 2013 does have the ability to view asynchronous causality stacks in the debugger. If you want similar behavior at runtime, then you can use my async diagnostics package.

like image 25
Stephen Cleary Avatar answered Oct 24 '22 17:10

Stephen Cleary