How can I get generic occurred errors in windows application or windows services like Application_Error in HttpApplication
How can I get generic occurred errors in windows application or windows services like
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use the following events to trap exceptions in Windows Forms applications and .Net Services:
AppDomain.FirstChanceException
This event is triggered whenever there is an exception in a given AppDomain, even if the event is later handled. This is almost certainly not what you want but I thought I’d include it for completeness.
Note that this is a per-AppDomain event – if you use multiple AppDomains then you need to handle this event for each one. If you only have a single AppDomain (more likely) then the following will handle this event:
AppDomain.UnhandledException
This event is triggered whenever there is an unhandled exception in a given AppDomain. Again this is a per-AppDomain event and is wired up in a similar way to the
FirstChanceExceptionevent.You can hook these two events at any point you like however you probably want to do this as soon as possible otherwise exceptions might be thrown before you hook up your event handler. The start of the
Mainmethod in yourProgramclass is normally a good place to do this.Note that in a Windows Forms application this event might not get triggered when you would otherwise expect it to as unhandled exceptions in Windows Forms applications are handled differently (by the Windows Forms infrastructure) and so are sometimes not propogated up as unhandled exceptions in the AppDomain.
Take a look at Why is my Catch block only running while Debugging in Visual Studio?
Application.ThreadException
(only for Windows Forms applications)
This occurs when an unhandled exception is thrown in a Windows Forms application which is then caught by the Windows Forms infrastructure. You should probably be using this instead of
AppDomain.UnhandledExceptionto catch unhandled excpetions in Windows Forms applications:Again you want to hook this up as soon as possible – the start of your
Mainmethod in theProgramclass is normally a good place to do this.Summary
Note that none of them are exactly like the
Application_Errormethod of an ASP.Net application however if you are creating a Windows Forms application then you probably want to useApplication.ThreadExceptionand if you are creating a Windows Service then you probably wantAppDomain.UnhandledException