不,那是正确的方法。这完全可以正常工作,也许您可以从以下方面进行工作:
using System;class Program { static void Main(string[] args) { System.AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper; throw new Exception("Kaboom"); } static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e) { Console.WriteLine(e.ExceptionObject.ToString()); Console.WriteLine("Press Enter to continue"); Console.ReadLine(); Environment.Exit(1); }}请记住,您无法通过这种方式捕获由抖动生成的类型和文件加载异常。它们发生在您的Main()方法开始运行之前。捕获这些错误需要延迟抖动,将有风险的代码移至另一种方法,然后将[MethodImpl(MethodImplOptions.NoInlining)]属性应用于该方法。



