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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:04:23+00:00 2026-06-10T22:04:23+00:00

When an async method that is awaited upon throws an exception, the exception is

  • 0

When an async method that is awaited upon throws an exception, the exception is stored somewhere and throwing it is delayed. In a WinForms or WPF application, it uses SynchronizationContext.Current to post throwing of the exception. However, in e.g. a console application, it throws the exception on a thread pool and it brings down the application.

How can I prevent exceptions thrown from an async method from bringing down the application?

EDIT:

Appearantly the issue I’m describing is because I have void async methods. See comments.

  • 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-06-10T22:04:25+00:00Added an answer on June 10, 2026 at 10:04 pm

    When the async method is started, it captures the current synchronization context. A way to solve this issue is to create your own synchronization context which captures the exception.

    The point here is that the synchronization context posts the callback to the thread pool, but with a try/catch around it:

    public class AsyncSynchronizationContext : SynchronizationContext
    {
        public override void Send(SendOrPostCallback d, object state)
        {
            try
            {
                d(state);
            }
            catch (Exception ex)
            {
                // Put your exception handling logic here.
    
                Console.WriteLine(ex.Message);
            }
        }
    
        public override void Post(SendOrPostCallback d, object state)
        {
            try
            {
                d(state);
            }
            catch (Exception ex)
            {
                // Put your exception handling logic here.
    
                Console.WriteLine(ex.Message);
            }
        }
    }
    

    In the catch above you can put your exception handling logic.

    Next, on every thread (SynchronizationContext.Current is [ThreadStatic]) where you want to execute async methods with this mechanism, you must set the current synchronization context:

    SynchronizationContext.SetSynchronizationContext(new AsyncSynchronizationContext());
    

    The complete Main example:

    class Program
    {
        static void Main(string[] args)
        {
            SynchronizationContext.SetSynchronizationContext(new AsyncSynchronizationContext());
    
            ExecuteAsyncMethod();
    
            Console.ReadKey();
        }
    
        private static async void ExecuteAsyncMethod()
        {
            await AsyncMethod();
        }
    
        private static async Task AsyncMethod()
        {
            throw new Exception("Exception from async");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a service hosted in a WPF application with an async method with
I have a tcp server that uses TCPListener and the async method BeginAcceptTCPClient: Imports
I have a piece of code (on a server) that uses async method to
I am working on a WPF application. I have a time consuming method that
I have a receive callback from an async client method that is supposed to
I have a public async Task Foo() method that I want to call from
Im trying to make an async method that returns a value. everything work when
How to wrap existing async method that accepts callback function as parameter into Task
I know how to make Async methods but say I have a method that
I made a simple async method to call an SQL stored procedure asynchronously. In

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.