Solved

C# async problem

Posted on 2014-03-25
15
306 Views
Last Modified: 2014-03-28
I'm trying to debug code with Task based async, but any error generated in the 'task' seems to bubble straight up the call stack.
I've tried Debug-Exceptiones/ and just clicked the CLR node but then it stops on everything...
0
Comment
Question by:Silas2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 4
15 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39953528
seems to bubble straight up the call stack.
Does the stack trace not tell you where the error is occurring?
0
 

Author Comment

by:Silas2
ID: 39953552
The stack trace would be great but as its not breaking on the error by the time you realise an exception is thrown, the call stack is for the handling code, not the faulting code.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39953591
I'm not clear what you mean.  Is the following roughly what is happening?
You have a try...catch block in foo.  In that try...catch block you call foo2, which calls foo3, which calls foo4 ..... which calls fooXXX and somewhere in that an exception is thrown.
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:Silas2
ID: 39953610
No, no try/catch in my code - I want to see all my errors!
I run a async task-based pattern to execute a procedure (with no try/catch block in), the procedure faults, I'm hoping to see which line, but the fault is caught in the task-asnyc calling bit, with no record of which line in my code threw the exception.

Its as if the async task pattern has a try/catch block on everything inside it so you can't trace the fault.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39953650
>>No, no try/catch in my code - I want to see all my errors!

Oh dear.  The whole point of try....catch is to trap an error AND DISPLAY IT IF REQUIRED at a point where you can easily locate what is the faulting code.
0
 

Author Comment

by:Silas2
ID: 39953684
I never use error handling for 'normal flow' in my code, just for unpredictable activities, like talking to the outside world, file I/O etc. Normal flow shouldn't throw errors, everything should be pre-empted by the logic.
That's a coding style I've been using for 20 years...but don't get me started on overuse of error handling code.
But that isn't germane to this issue really which is that the error handling in the Task/Async pattern in over-zealous and handling unhandled errors when I want to break there.
Toggling the Debug-Exceptions/CLR seems to be the only way I've found.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39953745
OK, I'll bow out to your expertise in writing error free code in that case.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39953813
When the error bubbles up, is there anything in the InnerException property?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39953821
I'll asked for this to be reopened.  That comment is not an answer.
0
 

Author Comment

by:Silas2
ID: 39953915
is there anything in the InnerException property?
There doesn't seem to be any exception thrown in the asnyc task (CLR) code, it's just catching my exception and ignoring it.

I am having to toggle the Debug-Exceptions-Thow All CLR check box - which is a tedious workaround...
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39954084
I can relate to having to flip back and forth through Debug->Exceptions.

As to your issue, have you read through the following yet?

http://msdn.microsoft.com/en-us/library/dd997415(v=vs.110).aspx

Shall we assume that the behavior you are witnessing is different than what this document proposes?
0
 

Author Comment

by:Silas2
ID: 39954113
I think the phenomenon is the one described in the document, but that doesn't really help when debugging, in the sense that the document suggests:
var task1 = Task.Factory.StartNew(() =>
{
    throw new MyCustomException("I'm bad, but not too bad!");
});

try
{
    task1.Wait();
}
catch (AggregateException ae)
{
    // Assume we know what's going on with this particular exception. 
    // Rethrow anything else. AggregateException.Handle provides 
    // another way to express this. See later example. 
    foreach (var e in ae.InnerExceptions)
    {
        if (e is MyCustomException)
        {
            Console.WriteLine(e.Message);
        }
        else
        {
            throw;
        }
    }

}

Open in new window

whereas, for debug you want the code to actually break on the error, not a fancy-pants aggregated exception handler in the calling code.
I'm sure the old async patterns allowed you to break on exceptions without having to toggle the 'break on All exceptions' box. I suppose I could make a macro, but I think the VS macro recorder's been removed.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 39954239
I'm not certain it will answer your question, but the following explains why the AggregateException exists:

http://blogs.msdn.com/b/pfxteam/archive/2011/09/28/task-exception-handling-in-net-4-5.aspx

The section discussing <ThrowUnobservedTaskExceptions enabled="true"/> may be what you are after, though.
0
 

Author Comment

by:Silas2
ID: 39954251
Aaahhhh, yes that's very interesting. Thanks for that. I'll have a play around.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

736 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question