Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get WinForms to stop silently ignoring unhandled exceptions?

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!

like image 353
KallDrexx Avatar asked Sep 27 '11 17:09

KallDrexx


People also ask

What happens with an unhandled exception C#?

An unhandled exception occurs when the application code does not properly handle exceptions. For example, When you try to open a file on disk, it is a common problem for the file to not exist. The . NET Framework will then throw a FileNotFoundException.

Which event is used for unhandled exceptions?

asax and the Application_Error event handler to execute code when an unhandled exception occurs.

How do I turn off WinForms full screen?

The only thing you can do is completely disable resizing your window. In WinForms, you do that by setting the FormBorderStyle property to one of the following: FormBorderStyle. FixedSingle , FormBorderStyle. Fixed3D , or FormBorderStyle.


2 Answers

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); } 
like image 187
Reddog Avatar answered Sep 30 '22 01:09

Reddog


Try the following.

  • Handle exceptions in your main application entry point.
  • Also, manage unhandled thread exceptions using a ThreadExceptionEventHandler

This is the code snippet:

[STAThread] public static void Main(string[] args) {     try     {         Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);         //your program entry point     }     catch (Exception ex)     {        //manage also these exceptions     } }  private void Application_ThreadException(object sender, ThreadExceptionEventArgs e) {     ProcessException(e.Exception); } 
like image 25
Daniel Peñalba Avatar answered Sep 30 '22 01:09

Daniel Peñalba