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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:59:06+00:00 2026-06-03T05:59:06+00:00

I am using following code to handle all the unhandled exceptions in the program.

  • 0

I am using following code to handle all the unhandled exceptions in the program. But the problem that the exception is not propagated to the specified methods.

 [STAThread]
    static void Main()
    {


        AppDomain currentDomain = default(AppDomain);
        currentDomain = AppDomain.CurrentDomain;
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
        // Handler for unhandled exceptions.
        currentDomain.UnhandledException += GlobalUnhandledExceptionHandler;
        // Handler for exceptions in threads behind forms.
        System.Windows.Forms.Application.ThreadException += GlobalThreadExceptionHandler;


        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new FormMain());
    }

  private static void GlobalUnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
    {
        Exception ex = (Exception)e.ExceptionObject;        

        MessageBox.Show(ex.Message,
                                  "Important",
                                  MessageBoxButtons.YesNo);
    }

    private static void GlobalThreadExceptionHandler(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
        Exception ex = e.Exception;

        MessageBox.Show(ex.Message,
                                 "Important",
                                 MessageBoxButtons.YesNo);

    }

The FormMain has a backgroundWorker object. This backgroundworker is the one who does most part of the app and if there is an exception in doWork method, it is not propogated as I expect it to ..
Here is code for the backgroundworker that I use in my MainForm.

 private void button_startSync_Click(object sender, EventArgs e)
 {            
     backgroundWorker1.RunWorkerAsync(getSettings());\          
 }

  private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  {        

     // ... processing code
     // Unhandled Exception will generated in this method...           
  }

Please advise what am I doing wrong here, I want exceptions generated anywhere in my program being caught into those global handlers above, so that the erro/exceptions be reported back for fixing..

Thanks,

  • 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-03T05:59:07+00:00Added an answer on June 3, 2026 at 5:59 am

    This is by design, the BackgroundWorker class catches any exceptions that are thrown inside the DoWork event handler. And exposes it in the e.Error property in the RunWorkerCompleted event handler. So write it like this:

      private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
      { 
          if (e.Error != null) throw e.Error;
          // etc..
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using following code but cannot return data from MySQL. This is the output:
I am using the following code: myObj jsonStream = ser.Deserialize<myObj>(jsonStream); And all was working
All, I'm using the following code to upload some images to my Wordpress blog:
I've been using the following code to get all combinations of a pre-determined amount
I have a problem with the following code fragment. It's intended to handle events
So I have the following code that I am using to create a form
I want to declare an array of handel as the following code: using namespace
Using following code I try to get updated list of checkbuttons' corresponding text values,
I using following code: var search = 'test'; if ($('#sku').find(search) ){ //alert(search); $(document).find(search).css('color','red'); <TABLE>
I am using following code to remove some specific nodes from xml file..It show

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.