views:

2170

answers:

4

I have a problem when an unhandeld exception occurs while debugging a WinForm VB.NET project.

The problem is that my application terminates and I have to start the application again, instead of retrying the action as was the case in VS2003

The unhandeld exception is implemented in the new My.MyApplication class found in ApplicationEvents.vb

Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
    Dim handler As New GlobalErrorHandler()
    handler.HandleError(e.Exception)

    e.ExitApplication = False
End Sub

Note: handler.HandleError just shows a dialog box and logs the error to a log file.

I also tried the following code that used to work in VS2003 but it results in the same behaviour when run in VS2008:

    AddHandler System.Windows.Forms.Application.ThreadException, AddressOf OnApplicationErrorHandler
    AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf OnUnhandledExceptionHandler

OnApplicationErrorHandler and OnUnhandledExceptionHandler does the same as handle.HandleError

Running the application outside VS2008 results in the expected behaviour (the application doesn't terminate) but it is increasing our test cycle during debugging.

Update: I have added sample code in my answer to demonstrate this problem in C#

A: 

I'm not sure about VS2008, but I had the same issue for awhile in VS2005. It turns out I just had to go to Debug->Exceptions (or Crtl + Alt + E) and make sure that all the Thrown boxes are unchecked, but all the User-unhandled boxes are checked.

You may have something different and funky going on with the code you have, but it may fix this behavior.

Jeffrey
I have tried what you suggested but it didn't work for me :(
Philip Fourie
A: 

OK I have done more work on this but still don't have an answer. I found that this problem is not only related to VB.NET but also to C#

Here is simple C# program that demonstrates the problem:

using System;
using System.Windows.Forms;
namespace TestCSharpUnhandledException
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        }

        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            MessageBox.Show("Oops an unhandled exception, terminating:" + e.IsTerminating);                
        }

        void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            MessageBox.Show("Oops an unhandled thread exception");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            throw new Exception("Dummy unhandled exception");
        }
    }
}

What I found interesting is debugging in VS the UnhandledException event is thrown but running the application outside VS causes the ThreadException to fire.

Also inside VS the e.IsTerminating is set to true, ultimately I want this to be false. (This is a read only property)

Philip Fourie
+1  A: 

Ok I found the answer to this issue in this blog post: Handling "Unhandled Exceptions" in .NET 2.0

Thank you Mark!

The short answer is:

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

This causes the same behaviour during debug and run mode. Resulting in VS not closing while debugging

Philip Fourie
A: 

Philip. for precise details of what's happening, see my answer here.

RoadWarrior