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 6025239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:16:20+00:00 2026-05-23T04:16:20+00:00

This example fails: static async void Main(string[] args) { try { await TaskEx.Run(() =>

  • 0

This example “fails”:

static async void Main(string[] args)
{
    try
    {
        await TaskEx.Run(() => { throw new Exception("failure"); });
    }
    catch (Exception)
    {
        throw new Exception("success");
    }
}

That is, the exception with the text “failure” bubbles up.

Then I tried this workaround:

static async void Main(string[] args)
{
    try
    {
        await SafeRun(() => { throw new Exception("failure"); });
    }
    catch (Exception)
    {
        throw new Exception("success");
    }
}

static async Task SafeRun(Action action)
{
    var ex = default(Exception);
    await TaskEx.Run(() =>
    {
        try
        {
            action();
        }
        catch (Exception _)
        {
            ex = _;
        }
    });
    if (ex != default(Exception))
        throw ex;
}

That didn’t help either.

I suppose my Async CTP refresh installation could be hosed.

Should this code work as I expect (“success” bubbles up, not “failure”), or is this not “supposed” to work that way. And if not, how would you work around it?

  • 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-23T04:16:21+00:00Added an answer on May 23, 2026 at 4:16 am

    The behavior you are seeing is likely an edge case bug or may even be correct, if unintuitive. Normally when you invoke an async method synchronously, it wraps a task around to execute and since there is no one waiting on the task to finish, the exception never makes it to the main thread. If you were to call Main directly it would succeed, but then your runtime would see an exception of “success” on another thread.

    Since main is the entrypoint of your application, it is invoked synchronously and likely as the entrypoint doesn’t trigger the Task wrapping behavior, so that await isn’t run properly and the TaskEx.Run throws on its own thread, which shows up in the runtime as an exception being thrown on another thread.

    If you were to run main as an async method, i.e. returning a Task (since an async that returns void can only really be called via await) and blocking on it from your synchronous main context, you would get the appropriate behavior as the below test illustrates:

    static async Task Main() {
        try {
            await TaskEx.Run(() => { throw new Exception("failure"); });
        } catch(Exception) {
            throw new Exception("success");
        }
    }
    
    static async Task Main2() {
        await Main();
    }
    
    [Test]
    public void CallViaAwait() {
        var t = Main2();
        try {
            t.Wait();
            Assert.Fail("didn't throw");
        } catch(AggregateException e) {
            Assert.AreEqual("success",e.InnerException.Message);
        }
        }
    
    
    [Test]
    public void CallDirectly() {
        var t = Main();
        try {
            t.Wait();
            Assert.Fail("didn't throw");
        } catch(AggregateException e) {
            Assert.AreEqual("success", e.InnerException.Message);
        }
    }
    

    I.e. the Task faults with an AggregateException which contains the success exception as it’s inner exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a contrived example: public static class MyExtensions { public static void MyMethod(
This example is from php.net: <?php function Test() { static $a = 0; echo
In this example the .Stub returns a new memory stream. The same memory stream
I have found this example on StackOverflow: var people = new List<Person> { new
Given this example: <img class=a /> <img /> <img class=a /> <img class=a id=active
Consider this example table (assuming SQL Server 2005): create table product_bill_of_materials ( parent_product_id int
Consider this example (typical in OOP books): I have an Animal class, where each
Following this example, I can list all elements into a pdf file import pyPdf
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
There is this example code, but then it starts talking about millisecond / nanosecond

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.