none
Exception in async methods RRS feed

  • Question

  • Consider the following code snippet:

            private async void Button_Click_1(object sender, RoutedEventArgs e)
            {
                try
                {
                    await WaitAsync();
                    MessageBox.Show("All went ok");
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                }
            }
    
            private async Task WaitAsync()
            {
                await Task.Run(() =>
                    {
                        System.Threading.Thread.Sleep(1000);
                        throw new Exception("This was thrown in WaitAsync");
                    });
            }
    

    The 2 lambda expression lines "simulates" a method call to some longlasting method on a COM Object, which may throw a COMException.

    How can this expression be caught in the calling thread to display the message ?
    MessageBox.Show(exception.Message);

    Thursday, August 30, 2012 2:28 PM

Answers

  • Have you tried this code and you didn't catch the exception in Button_Click_1? Because it's supposed to work and it works for me.
    • Proposed as answer by Sachin P Thursday, August 30, 2012 4:49 PM
    • Marked as answer by Mike FengModerator Thursday, September 6, 2012 6:48 AM
    Thursday, August 30, 2012 3:17 PM
    Moderator
  • "do you know if it is possible to mark the call with something like an attribute [DebuggerBreakIfException(false)] or similar "

    Not that I know of. The only to change such stuff is from Debugger\Exceptions menu. But you seem to know about that already.

    Thursday, August 30, 2012 4:38 PM
    Moderator

All replies

  • Have you tried this code and you didn't catch the exception in Button_Click_1? Because it's supposed to work and it works for me.
    • Proposed as answer by Sachin P Thursday, August 30, 2012 4:49 PM
    • Marked as answer by Mike FengModerator Thursday, September 6, 2012 6:48 AM
    Thursday, August 30, 2012 3:17 PM
    Moderator
  • Thanks a lot.
    Of course you are right, it is working perfectly well.
    I was just fooled by the debugger which broke the execution while debugging, as it should.
    Removing break for the exception in question (Exception in the simulated case, COMException in the real case) solves this "problem".

    B.t.w. do you know if it is possible to mark the call with something like an attribute [DebuggerBreakIfException(false)] or similar (I do not like the general idea of not breaking on a certain exception class) ?
    (Though I think attributes do not have any place on method calls ?)

    Thursday, August 30, 2012 4:26 PM
  • "do you know if it is possible to mark the call with something like an attribute [DebuggerBreakIfException(false)] or similar "

    Not that I know of. The only to change such stuff is from Debugger\Exceptions menu. But you seem to know about that already.

    Thursday, August 30, 2012 4:38 PM
    Moderator