Full of sincerity directly on the code:
Static void Main (string[] args) { ///main function Add this sentence new Unhandledexceptioneventhandler (currentdomain_unhandledexception);}
In the program class (the class where the main function is located), add the following function:
Private Static void Currentdomain_unhandledexception (object sender, UnhandledExceptionEventArgs e) { Console.WriteLine (E.exceptionobject.tostring ()); Environment.exit (-1// have this sentence do not play the Exception dialog box }
Note: If you print an exception in currentdomain_unhandledexception and do not terminate the program, the exception will still be thrown, eventually causing the "XXX has stopped working" dialog box, so if you do not want the box to get stuck, you need to use the second sentence Environment.exit or other means of terminating the program. Of course, in addition to the command line output exception can also save the current data or other disaster treatment measures, but because the program has been abnormal, there may be some data is corrupted, read may lead to other problems, be careful oh!
In addition, the WinForm program, the Application object to do some settings, specifically, you can refer to the Microsoft Documentation Example: https://msdn.microsoft.com/zh-cn/library/ms157905 (v=vs.110) . aspx
C # program How to catch an Try/catch exception--Do not play "XXX has stopped working" error box