问题
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:
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.
来源:https://stackoverflow.com/questions/62228080/unhandled-exception-message-not-shown