我使用的示例代碼是this MSDN article。.NET未處理的異常
[SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlAppDomain)]
public static void Main()
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
try {
throw new Exception("1");
} catch (Exception e) {
Console.WriteLine("Catch clause caught : {0} \n", e.Message);
}
throw new Exception("2");
}
static void MyHandler(object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception) args.ExceptionObject;
Console.WriteLine("MyHandler caught : " + e.Message);
Console.WriteLine("Runtime terminating: {0}", args.IsTerminating);
}
該處理程序捕獲未處理的異常。但是,在處理程序運行後,未處理的異常2仍然顯示。在調試模式下,在發佈模式下以及是否直接啓動.exe。
我想壓制第二個異常,所以未處理的異常處理程序可以終止/重新啓動應用程序。我記得使用VB.NET在.NET 3中工作。
請將代碼發佈在您的帖子中。鏈接到代碼很難遵循和回覆。 – Hank
你應該更努力地尋找模式。如果'try catch'有助於例外#1,那麼猜猜對於例外#2 ...會有什麼幫助? –