Click here to Skip to main content
15,891,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I've a winforms applicatoin (a big one with 1 mdi and about 100+ forms). I've added code in applicationevents.vb to handle the unhandled exceptions.

VB
Namespace My
    Partial Friend Class MyApplication

        Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
            e.ExitApplication = False
            IO.File.AppendAllText("error.txt", Format(Now, "dd-MMM-yyyy HH:mm:ss") & " - Unex - " & e.Exception.Message & vbCrLf & _
                                  e.Exception.StackTrace & vbCrLf)
            MessageBox.Show("got error")
        End Sub

    End Class

End Namespace


Here I'm able to get the message box, and the exception details written to the txt file. But the application closes, even though I explicitly coded ExitApplication to False.

When I test the scenario in a simple winform, it works fine. But in this project , I'm facing this problem. Is there any special configuration I'm missing out?

Any help/ idea greatly appreciated.
Posted

This is not how you should do it. You need to handle all exceptions in every outer event handling loop of the application. For this purpose, System.Windows.Forms.Application class has the following facility:
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.onthreadexception%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.threadexceptioneventhandler%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.threadexceptioneventargs%28v=vs.110%29.aspx[^],
see also http://msdn.microsoft.com/en-us/library/ms157905(v=vs.110).aspx[^],
or http://msdn.microsoft.com/en-us/library/ms223898(v=vs.110).aspx[^].

First of all, in your entry-point method (Main), you need to set up this mode:
VB
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
' see other values of this enum

And then, install an exception handler for the event Application.OnThreadException somewhere. I usually do it in the constructor of the main form, in the very beginning, just to use some form methods in the handler, but it could be done anywhere else, as soon as possible. The event arguments class System.Threading.ThreadExceptionEventArgs gives your the access to the exception instance (ThreadExceptionEventArgs.Exception). The sample code can be found here:
http://msdn.microsoft.com/en-us/library/system.threading.threadexceptioneventargs%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
v4
Comments
CHill60 9-Jan-14 13:02pm    
5'd!
Sergey Alexandrovich Kryukov 9-Jan-14 13:04pm    
Thank you very much,
—SA
[no name] 9-Jan-14 14:44pm    
Learning++
Sergey Alexandrovich Kryukov 9-Jan-14 23:33pm    
Thank you.
—SA
thams 10-Jan-14 1:00am    
Hi,
Thanks for the bunch of tech stuffs.
I've identified my problem. Just made the jit debugger = true in the app config file. This was the culprit. Making it false (or simply delete the line) solved the problem.
Thanks anyway.
In the app.config file

HTML
<system.windows.forms jitdebugging="true" />

I coundn't remember when I've added this. Making it to false solved the problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900