class Program { static int m_count = 0; static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; while(true) { Func1(); Thread.Sleep(10); } } private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { Console.WriteLine("有未经处理的异常,程序即将退出!"); Thread.Sleep(5000); Environment.Exit(-1); } static void Func1() { m_count++; if(m_count > 100) { throw new Exception(); } } }
结果: