4

I made a application in VB.Net. But some users face crash upon startup. That is "A problem caused this program from working correctly" with just one button "Close the program". Since there are lot of things happening when the app loads, is it possible to know what caused the issue?

1 Answer 1

6

If the "Application Framework" is enabled in your project's properties, click the "View Application Events" button on the "Application" project properties page. Then add an event handler:

Partial Friend Class MyApplication Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException ' ... End Sub End Class 

If you are not using the application framework, you should put a try catch block around your entire Main method. However, that will only catch exceptions that occur on the primary thread. If your application is multi-threaded, you can handle exceptions from all threads by creating a method like this:

Public Sub UnhandledExceptionHandler(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs) ' ... End Sub 

And then attach it to your current domain's UnhandledException event, like this:

AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledExceptionHandler 

That event handler will then get called for all unhandled exceptions from anywhere in your domain, regardless of the current thread.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot SteveDog. Now is there anyway to force unhanded exception to see the error?
I'm not sure what your question is. If an exception is thrown and it is unhandled, then the MyApplication_UnhandledException handler should receive it. I believe it is possible that you could have unhandled exceptions on other threads or something which won't get handled by that exception handler, but anything thrown on the UI thread should be automatically caught by that handler.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.