Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7062631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:37:02+00:00 2026-05-28T04:37:02+00:00

I have an Async method returning a Task. I also wish to offer a

  • 0

I have an Async method returning a Task.

I also wish to offer a synchronous equivalent, but I don’t want consumers of it to have to go unpacking AggregateExceptions.

Now I understand that the whole idea is that you couldn’t arbitrarily pick one in a general way, and I know I could go read loads more Stephen Toub articles (I will, but not now) and I’ll understand it all and can decide for myself.

In the interim, I want to use the fact that my tasks are actually just chained ‘workflows’ without parallelism, just intervening Waits (no, not TPL DataFlow) which shouldnt not result in more than one exception. In that case, would it be appropriate to handle as follows:

    CallAsync().Wait();
}
catch( AggregateException ae)
{
    throw ae.Flatten().First()

or am I guaranteed that an AggregateException always has an InnerException even if there are more than one. Or is there a case where I should fall back to .Flatten().First() ?


In some TPL docs, I see a reference to an Unwrap() method on AggregateException (not sure if it was an extension or something in a beta release).

As a placeholder, I’m doing:

void Call( )
{
    try
    {
        CallAsync().Wait();
    }
    catch ( AggregateException ex )
    {
        var translated = ex.InnerException ?? ex.Flatten().InnerExceptions.First();
        if ( translated == null )
            throw;
        throw translated;                 }
}

Task CallAsync(){ ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:37:03+00:00Added an answer on May 28, 2026 at 4:37 am

    There’s no “clean” way to do this that I know of. You can’t use throw someInnerException; because you’ll lose the stack wherever the exception originated in the the async workflow and if you just use throw; you’re obviously going to propagate the AggregateException. What you would have to do for the synchronous method is have some kind of “wrapper” exception that you can stuff the first exception of the AggregateException into and then throw that consistently from the synchronous version of the method.

    void Call()
    {
        try
        {
            CallAsync().Wait();
        }
        catch (AggregateException ex)
        {
            throw new MyConsistentWrapperException("An exception occurred while executing my workflow. Check the inner exception for more details.", ex.Flatten().InnerExceptions.First());
        }
    }
    

    FWIW, they’ve solved this in 4.5 with the new ExceptionDispatchInfo class which will help you marshal exceptions across threads without whacking the stack. Then you could write the synchronous version like this:

    void Call()
    {
        try
        {
            CallAsync().Wait();
        }
        catch (AggregateException ex)
        {
            ExceptionDispatchInfo.Capture(ex.Flatten().InnerExceptions.First()).Throw();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know how to make Async methods but say I have a method that
Why have the calling thread walk into the async method until the inner 'await'?
How to wrap existing async method that accepts callback function as parameter into Task
I have a tcp server that uses TCPListener and the async method BeginAcceptTCPClient: Imports
I have this simple method of deleting list of files. But for some cases
Is it bad to have expensive code at the start of an async method,
I have an async method which puts markers on a Google Map View. In
I have a piece of code (on a server) that uses async method to
I have a NetworkStream which I read asynchronously (using async/await) await Task<int>.Factory.FromAsync((cb, state) =>
I have an async queue processor that has a Run method which keeps running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.