0

I created Windows Form Application from Blank Project Template. It seems like the .Net Unhandled Exception not enabled by default.

It was noticed just when the application is almost done and tried on the target computer. It crashed without any Unhandled Exception message.

It is understood that, if it was created by Win Form Application Project, the .Net Unhandled Exception is enabled by default.

How can we enable it for the Existing project?

Is There a way, or shall we recreate New Project from the Template?

1 Answers1

1

In order to handle application events, you need to enable the VB Application Framework, which you cannot do for a project based on the Empty Project template. If you double-click on the My Project node in the Solution Explore and select the Application page, you'll see that the Enable application framework check box is disabled.

To enable that box, close your project in VS and then open the VBPROJ file from the project folder in an editor. You could use Notepad but something like VS Code is a bit nicer. In that file, find the line in the first PropertyGroup element that looks like this:

<MyType>Empty</MyType>

That's what specifies the project template and you need to change it to this:

<MyType>WindowsForms</MyType>

Save the file and open your project in VS again. If you had the project property pages open then you may get an error message displayed. If so, close that document window and reopen it. You should now see that check box is enabled, so you can check it and then click the View Application Events button and create a handler for the UnhandledException event.

Just be aware that you must have selected a form as your startup object for the project in order to enable the Application Framework. If you already have your own Main method then you will not be able to use that any more. With the Application Framework enabled, VB creates its own Main method that is hidden from you. Any code you need executed at startup that would normally go in a Main method should be moved to the Startup event handler.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • In My Case, the _Application framework check box_ is enabled and I checked it already. May be it is because I created the blank project under _Visual Basic_ and _Desktop_ filter, I'm not sure. However when I clicked the _View Application Event_ button, an **ApplicationEvent.vb** appears with only a blank `Partial Friend Class MyApplication` within `Namespace My`. And this is the same view as the other project created by _WinForms Project_. No `UnhandledException` code there but mentioned after comment mark. Don't know what to do then? – Haitham Sungkar Jun 07 '20 at 04:02
  • What you do is what I said to do in my answer, i.e. create a handler for the `UnhandledException` event. You have to do that no matter what. No event is handled unless you handle it. In any code window - this one or a form or any other - you use the navigation bar at the top to create event handlers. – jmcilhinney Jun 07 '20 at 04:06
  • Well, Ok. And I expect the default .NET framework Unhandled Exception message box to appear when an Unhandled Exception occur. what code should I write under `MyApplication_UnhandledException` ? – Haitham Sungkar Jun 07 '20 at 23:49
  • The whole point of that event is that you get to decide what to do. The only default message you could get is one that tells you that your app has crashed. The point of the `UnhandledException` event is that your app does NOT crash. You get to log the exception, notify the user and then either shut down cleanly (default), continue running as is (NOT recommended) or explicitly restart the application. The code you should write is the code you want executed. – jmcilhinney Jun 08 '20 at 03:35
  • 1
    Easy way to get a nice project file editor if you are using Visual Studio: Unload the project, then right-click on the project and select "Edit (project file name)". Then, when you're done, you can reload the project, all without exiting Visual Studio. If the project is your startup project, VS will temporarily switch to another project when you unload, but the original setting will be restored when you reload. – Craig Jun 08 '20 at 15:57