How can I get generic occurred errors in windows application or windows services like Application_Error
in HttpApplication

- 675
- 9
- 29
-
You want to view the list of exceptions? – Abdul Munim Nov 15 '11 at 13:18
-
I need a method that called when an error occurred in my application – Mehdi Hadjar Nov 15 '11 at 13:19
2 Answers
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.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;
static void CurrentDomain_FirstChanceException(object sender, FirstChanceExceptionEventArgs e)
{
throw new NotImplementedException();
}
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 FirstChanceException
event.
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
throw new NotImplementedException();
}
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 Main
method in your Program
class 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.UnhandledException
to catch unhandled excpetions in Windows Forms applications:
Application.ThreadException += Application_ThreadException;
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
throw new NotImplementedException();
}
Again you want to hook this up as soon as possible - the start of your Main
method in the Program
class is normally a good place to do this.
Summary
Note that none of them are exactly like the Application_Error
method of an ASP.Net application however if you are creating a Windows Forms application then you probably want to use Application.ThreadException
and if you are creating a Windows Service then you probably want AppDomain.UnhandledException
-
Handling `AppDomain.FirstChanceException` is an excellent development tool as it provides a means of checking for performance or correctness of application even when the exception has been handled. For instance you can log this event and look for things that are out place (weird exceptions, stack frames) and you can also step through from the point of this handler. Also a great way of finding tricky bugs related to empty try catch handlers whereby the underlying exception is actually swallowed and then code carries on in some weird path. Handling FCE gets you ahead of this and can be vhelpful. – rism May 22 '15 at 10:20
You can subscribe to the UnhandledException event like this:
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
An have a method in your code like this:
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
// DoSomething
}

- 12,238
- 2
- 39
- 56
-
Thanks I needed exactly this event, but where I can subscribe this method? – Mehdi Hadjar Nov 15 '11 at 13:29
-
-
I should use this method in all classes in my project to get errors? – Mehdi Hadjar Nov 15 '11 at 13:38
-
@MehdiHadjar: No sir, you *can* us it in any class you want, you *must* not use it in any class! If you are a newbie in C#, you should read the basics, please. – Fischermaen Nov 15 '11 at 14:08
-
It's my first windows application, anyway as Justin said I subscribe in Main method in the Program class. – Mehdi Hadjar Nov 15 '11 at 14:31