Genralized way to handle exceptions

G

Guest

Is there a generalized way to catch exceptions for a form/window? With MFC, I could implement a try/catch in an override of PreProcessMessage(..), or WndProc(..), but with Forms, those don't seem to work the same way and so the try/catch misses the exception

I'm trying to find a way to avoid implementing try/catch in every event handler for the form/window, since I handle most of them in the same way.
 
H

Herfried K. Wagner [MVP]

G

Guest

I'm trying to catch the error before it leaves the form so I can close the form but keep the application running. The ThreadException handler catches the exception at the application level when it's too late to do anything except shut down the application.
 
J

Jay B. Harlow [MVP - Outlook]

Bob,
No it doesn't! Herfried is correct, the Application.ThreadException event
handles exceptions that occur during the handling of an event, I suspect you
are thinking of the AppDomain.UnhandledException event.

Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

Hope this helps
Jay

Bob Kirkwood said:
I'm trying to catch the error before it leaves the form so I can close the
form but keep the application running. The ThreadException handler catches
the exception at the application level when it's too late to do anything
except shut down the application.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top