Tuesday, November 15, 2011

Handle the unhandled exception in application in c#

Unhandled exception within an application should be caught managed in a consistent and safe manner.This can be best achieved by using global safe error handler that can be trap all unhandled exception ,log the details ,then present a safe error page to the user.The unhandled exception handling is very important to  development any application . Mainly the unhandled exception are arises at database connection,file loading etc.These exceptions are come when the data base connection state is closed and file is not found.For this i have shown a solution to  handle the unhandled exception

This fallowing piece  of code  have to place in main () of entire application
AppDomain.CurrentDomain.UnhandledException +=UnhandledException.UnhandledExceptionTrapper;
Code behind:
Then we will create a separate class to trap the unhandled exception

public static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e)
      {
          Console.WriteLine(e.ExceptionObject.ToString());
          Console.WriteLine("Press Enter to continue");
          Console.ReadLine();
          Environment.Exit(1);
      } 
Post a Comment

Bel