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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:52:44+00:00 2026-05-25T19:52:44+00:00

This is getting extremely irritating. Right now I have a winforms application, and things

  • 0

This is getting extremely irritating. Right now I have a winforms application, and things were not working right, but no exceptions were being thrown as far as I could tell. After stepping through almost all pieces of relevant code, it turns out that an exception was being thrown at the start of my application.

Long story short, in WinForms, being as awesome as it is, if an exception occurs the WinForms library ignores it. No “an unhandled exception has occurred” JIT message is thrown, it just stops processing the current event and goes back to the GUI.

This is causing random bugs, because code to load data isn’t being called due to the exception occurring prior to this data being loaded.

To see this in action I created a brand new WinForms application, and entered the following code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string blah = null;
        blah.Trim();
    }
}

Press F5 and the form loads without any errors showing, even though a null reference is being thrown.

I then tried to go to my Program.cs main method and add Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException); to it. Still my form loads without causing any errors to be thrown.

Even though I know that I can tell VS to break on all exceptions, I find this situation really bad. It causes really wierd issues that are hard to debug in production, and as this is an internal tool I really want to have it so it actually errors out when an exception occurs, and not silently disregards it.

Does anyone know how to do this?


Update: Just to update on things I have learned from the comments.

This does appear to be a 64-bit issue with windows, as I learned from this question which I did not see before posting. In that question it pointed to a Microsoft bug report about this, which had this to say:

Hello,

This bug was closed as “External” because this behavior results from how x64 version of Windows handle exceptions. When a user mode exception crosses a kernel transition, x64 versions of Windows do not allow the exception to propagate. Therefore attached debuggers are unaware of the fact that an exception occured resulting in the debugger failing to break on the unhandled exception.

Unfortunately where is nothing that the Visual Studo team can do to address this, it is the result of operating system design. All feedback regarding this issue should be addressed to the Windows team; however the Windows team considers this to be the “correct” operating system design, and considers the x86 behavior to be “incorrect”.

Best Regards,
Visual Studio Debugger

That being said, builds not run through visual studio (or using Ctrl+F5 to run) does seem to show the JIT exception message box EXCEPT if you have the following code in your Program.cs:

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);

That code will cause windows to ignore the exception.

However, if you (instead) subscribe to the Application.ThreadException event, not only will your exceptions be caught, visual studio’s debugger will break on unhandled exceptions!

  • 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-25T19:52:44+00:00Added an answer on May 25, 2026 at 7:52 pm

    In your Program.cs’ Main function you should also ensure that you’ve wrapped your call to open the form in a try/catch. Additionally use the AppDomain.UnhandledException to catch exceptions. We also add Application.ThreadException too.

    I believe the following will give you hooks into all the exceptions that can be thrown…

    static void Main()
    {
        try
        {
            System.Windows.Forms.Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            System.Windows.Forms.Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(OnGuiUnhandedException);
            AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
    
            var form = new MainForm();
            form.ShowDialog();
        }
        catch (Exception e)
        {
            HandleUnhandledException(e);
        }
        finally
        {
            // Do stuff
        }
    }
    
    private static void HandleUnhandledException(Object o)
    {
        // TODO: Log it!
        Exception e = o as Exception;
    
        if (e != null)
        {
    
        }
    }
    
    private static void OnUnhandledException(Object sender, UnhandledExceptionEventArgs e)
    {
        HandleUnhandledException(e.ExceptionObject);
    }
    
    private static void OnGuiUnhandedException(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
        HandleUnhandledException(e.Exception);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(extremely) inexperienced in C++ but I've been getting along ok until now and this
So I have this extremely memory-intensive Java application I'm building, which builds a tree
This might be extremely trivial, and if so I apologise, but I'm getting really
I am getting this error now that I hit version number 1.256.0: Error 4
This is extremely basic and I apologize for asking such a rookie question. But
This is one of this things that should be extremely simple and I just
I am extremely confused why I am getting this strange error all the sudden:
I have an extremely simple page that isn't displaying properly in IE6. In this
I know this sounds very dumb, but we have a client that sends out
I have inherited an application that throws up 100 threads at the start. This

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.