Catch a completely unexpected mistake

I have an ErrorRecorder application that displays an error report and asks if the user wants to send this report to me.

Then I have the main application. If an error occurs, it writes the error report to the file and asks ErrorRecorder to open this file to show the user the error report.

So, I caught most of my mistakes using Try / Catch.

However, if an error occurred that was completely unexpected, and it disables my program.

Like the Global / Override method or something like that tells the program "Before shutting down, if an unexpected error occurs, call the ErrorRecorderView () method"

+2
source share
1 answer

I think this is what you need - you can handle exceptions at the domain level, i.e. through the whole program.
http://msdn.microsoft.com/en-GB/library/system.appdomain.unhandledexception.aspx

using System; using System.Security.Permissions; public class Test { [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)] public static void Example() { AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler); try { throw new Exception("1"); } catch (Exception e) { Console.WriteLine("Catch clause caught : " + e.Message); } throw new Exception("2"); // Output: // Catch clause caught : 1 // MyHandler caught : 2 } static void MyHandler(object sender, UnhandledExceptionEventArgs args) { Exception e = (Exception)args.ExceptionObject; Console.WriteLine("MyHandler caught : " + e.Message); } public static void Main() { Example(); } 

}

+5
source

All Articles