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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:15:58+00:00 2026-05-28T01:15:58+00:00

I am familiar with the fact that exception thrown in a thread normally couldn’t

  • 0

I am familiar with the fact that exception thrown in a thread normally couldn’t be caught in another thread.
How can i transfer the error however to the main thread?

public static void Main()
{
   new Thread (Go).Start();
}

static void Go()
{
  try
  {
    // ...
    throw null;    // The NullReferenceException will get caught below
    // ...
  }
  catch (Exception ex)
  {
    // Typically log the exception, and/or signal another thread
    // that we've come unstuck
    // ...
  }
}
  • 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-28T01:15:58+00:00Added an answer on May 28, 2026 at 1:15 am

    If you’re using .NET 4 there are better ways to do this with Tasks, but assuming you need to use Threads…

    If your example is a console app, then your Main method will exit, possibly before Go starts executing. So your “main thread” may not exist when the exception is thrown. To stop this, you need some synchronization.

    Something like this should do:

    static Exception _ThreadException = null;
    
    public static void Main()
    {
        var t = new Thread ( Go );
        t.Start();
    
        // this blocks this thread until the worker thread completes
        t.Join();
    
        // now see if there was an exception
        if ( _ThreadException != null ) HandleException( _ThreadException );
    }
    
    static void HandleException( Exception ex )
    {
        // this will be run on the main thread
    }
    
    static void Go()
    {
        try
        {
            // ...
            throw null;    // The NullReferenceException will get caught below
            // ...
        }
        catch (Exception ex) 
        {
            _ThreadException = ex;
        }
    }
    

    If this is a UI app, things are a bit easier. You will need to pass some reference to your UI thread into the Go method so it knows where to send the exception. The best way to do this is to pass the SynchronizationContext of the UI thread.

    Something like this would work:

    public static void Main()
    {
        var ui = SynchronizationContext.Current;
        new Thread ( () => Go( ui ) ).Start();
    }
    
    static void HandleException( Exception ex )
    {
        // this will be run on the UI thread
    }
    
    static void Go( SynchronizationContext ui )
    {
        try
        {
            // ...
            throw null;    // The NullReferenceException will get caught below
            // ...
        }
        catch (Exception ex) 
        {
            ui.Send( state => HandleException( ex ), null );
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm familiar with the fact that you can create LaTeX reports directly from R
I familiar with the fact that I should set the HTTP_RPOXY environment variable to
Being very familiar with the subversion workflow and that fact that 99.9% of the
I am familiar with the fact that an interface contains only the signatures of
Anyone familiar with error below? When I run my webapp to generate a dynamic
Anyone familiar with a way how can I can i write something to the
I am familiar with nunit for unit testing of the business layer however I
Although I couldn't find a reference to this easily in google, I'm familiar with
Pardon me for the woolly question - I'm not really that familiar with OLAP
I am hearing that sencha in general, by the mere fact of using javascript,

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.