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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:44:02+00:00 2026-05-21T23:44:02+00:00

One of my method ( Method1 ) spawns a new thread. That thread execute

  • 0

One of my method (Method1) spawns a new thread.
That thread execute a method (Method2) and during exectution an exception is thrown.
I need to get that exception information on the calling method (Method1)

Is there someway I can catch this exception in Method1 that is thrown in Method2?

  • 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-21T23:44:02+00:00Added an answer on May 21, 2026 at 11:44 pm

    In .NET 4 and above, you can use Task<T> class instead of creating new thread. Then you can get exceptions using .Exceptions property on your task object.
    There are 2 ways to do it:

    1. In a separate method: // You process exception in some task’s thread

      class Program
      {
          static void Main(string[] args)
          {
              Task<int> task = new Task<int>(Test);
              task.ContinueWith(ExceptionHandler, TaskContinuationOptions.OnlyOnFaulted);
              task.Start();
              Console.ReadLine();
          }
      
          static int Test()
          {
              throw new Exception();
          }
      
          static void ExceptionHandler(Task<int> task)
          {
              var exception = task.Exception;
              Console.WriteLine(exception);
          }
      }
      
    2. In the same method: // You process exception in the caller’s thread

      class Program
      {
          static void Main(string[] args)
          {
              Task<int> task = new Task<int>(Test);
              task.Start();
      
              try
              {
                  task.Wait();
              }
              catch (AggregateException ex)
              {
                  Console.WriteLine(ex);    
              }
      
              Console.ReadLine();
          }
      
          static int Test()
          {
              throw new Exception();
          }
      }
      

    Note that the exception which you get is AggregateException. All real exceptions are availible through ex.InnerExceptions property.

    In .NET 3.5 you can use the following code:

    1. // You process exception in the child’s thread

      class Program
      {
          static void Main(string[] args)
          {
              Exception exception = null;
              Thread thread = new Thread(() => SafeExecute(() => Test(0, 0), Handler));
              thread.Start();            
      
              Console.ReadLine();
          }
      
          private static void Handler(Exception exception)
          {        
              Console.WriteLine(exception);
          }
      
          private static void SafeExecute(Action test, Action<Exception> handler)
          {
              try
              {
                  test.Invoke();
              }
              catch (Exception ex)
              {
                  Handler(ex);
              }
          }
      
          static void Test(int a, int b)
          {
              throw new Exception();
          }
      }
      
    2. Or // You process exception in the caller’s thread

      class Program
      {
          static void Main(string[] args)
          {
              Exception exception = null;
              Thread thread = new Thread(() => SafeExecute(() => Test(0, 0), out exception));
      
              thread.Start();            
      
              thread.Join();
      
              Console.WriteLine(exception);    
      
              Console.ReadLine();
          }
      
          private static void SafeExecute(Action test, out Exception exception)
          {
              exception = null;
      
              try
              {
                  test.Invoke();
              }
              catch (Exception ex)
              {
                  exception = ex;
              }
          }
      
          static void Test(int a, int b)
          {
              throw new Exception();
          }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one method that opens a file and passes off to another function
Is there one method or property in the .NET Framework that tells if an
I am entering a realm that is new to me, but basically I need
I have one worker role that throws data into around 10 queues that need
I have written an HttpModule that spawns a background thread. I'm using the thread
One method which I can think of is to reverse the list and then
I modified one method of JQuery UI Resizable to achieve a centered resize. This
I have one method in my managed bean which returns javascript as a string.
I have one method with one letter(string\char?) as argument and one word active at
WCF service has one method ( Let's say TestMethod) in which I try to

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.