Thursday, 29 November 2012

Global Exception Handling in different types of applications

1. Asp.net

System.Web.HttpApplication.Error event

Usually setup in Global.asax

MSDN: How to: Handle Application-Level errors.
Also see:
Complete example for Error Handlers

2. WinForms:

System.Windows.Forms.Application.ThreadException
event
MSDN: Application.ThreadException event
Blog: Top-Level Exception Handling In Windows Forms Applications
// Add the event handler for handling UI thread exceptions to the event.
Application.ThreadException += new ThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException);

// Set the unhandled exception mode to force all Windows Forms errors to go through
// our handler.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

// Add the event handler for handling non-UI thread exceptions to the event. 
AppDomain.CurrentDomain.UnhandledException +=
        new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

3. Console applications (also should work with Windows Services – havent checked this though):
System.AppDomain.UnhandledException event
MSDN: AppDomain.UnhandledException Event

AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
4. WPF:

System.Windows.Application.DispatcherUnhandledException

MSDN: Application.DispatcherUnhandledException Event
public App() :base() //App.xaml.cs
{
        this.Dispatcher.UnhandledException += OnDispatcherUnhandledException;
}

Also see: WPF Global Exception Handler (StackOverflow)

5. Silverlight:

Application.UnhandledException Event

MSDN: Application.UnhandledException Event
Also see:
Silverlight Error Handling
public App()//App.xaml.cs
{
        this.UnhandledException += this.Application_UnhandledException;
        InitializeComponent();
}
private void Application_UnhandledException(object sender,ApplicationUnhandledExceptionEventArgs e)
{
        //handle exception here.
}
Also take a look at the EntLib Patterns and Practices pack for Silverlight (codeplex.com) {Logging Application Block}

No comments:

Post a Comment