Last active
December 26, 2015 15:19
-
-
Save yemrekeskin/7171725 to your computer and use it in GitHub Desktop.
UnhandledException
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // in console application | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| AppDomain currentDomain = AppDomain.CurrentDomain; | |
| currentDomain.UnhandledException += currentDomain_UnhandledException; | |
| Console.ReadLine(); | |
| } | |
| static void currentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) | |
| { | |
| Exception ex = (Exception)e.ExceptionObject; | |
| // logging and warning messages | |
| Console.WriteLine("MyHandler caught : " + ex.Message); | |
| Console.WriteLine("Runtime terminating: {0}", e.IsTerminating); | |
| } | |
| } | |
| // in windows form application | |
| static class Program | |
| { | |
| /// <summary> | |
| /// The main entry point for the application. | |
| /// </summary> | |
| [STAThread] | |
| static void Main() | |
| { | |
| Application.ThreadException += Application_ThreadException; | |
| Application.EnableVisualStyles(); | |
| Application.SetCompatibleTextRenderingDefault(false); | |
| Application.Run(new Form1()); | |
| } | |
| static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) | |
| { | |
| Exception ex = e.Exception; | |
| MessageBox.Show(ex.Message); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment