Mehdi Hadjar
Mehdi Hadjar

Reputation: 675

Get generic windows application error

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

Upvotes: 1

Views: 1125

Answers (2)

Justin
Justin

Reputation: 86729

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();
}

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 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

Upvotes: 4

Fischermaen
Fischermaen

Reputation: 12458

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
}

Upvotes: 1

Related Questions