2

我有一個ErrorRecorder應用程序,該應用程序打印錯誤報告並詢問用戶是否要將該報告發送給我。捕獲完全意外的錯誤

然後,我有主應用程序。如果發生錯誤,它會將錯誤報告寫入文件並要求ErrorRecorder打開該文件以向用戶顯示錯誤報告。

所以我使用Try/Catch捕獲了大部分錯誤。

但是,如果發生的錯誤完全出乎意料,它會關閉我的程序。

有沒有像全球/重寫方法或那樣的東西,這告訴程序「關停如果發生意外錯誤之前,調用‘ErrorRecorderView()’方法」

回答

5

我認爲這是什麼你在之後 - 你可以在應用程序級別處理異常 - 即在整個程序中處理異常。
http://msdn.microsoft.com/en-GB/library/system.appdomain.unhandledexception.aspx

using System; 
using System.Security.Permissions; 

public class Test 
{ 

[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)] 
public static void Example() 
{ 
    AppDomain currentDomain = AppDomain.CurrentDomain; 
    currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler); 

    try 
    { 
     throw new Exception("1"); 
    } 
    catch (Exception e) 
    { 
     Console.WriteLine("Catch clause caught : " + e.Message); 
    } 

    throw new Exception("2"); 

    // Output: 
    // Catch clause caught : 1 
    // MyHandler caught : 2 
} 

static void MyHandler(object sender, UnhandledExceptionEventArgs args) 
{ 
    Exception e = (Exception)args.ExceptionObject; 
    Console.WriteLine("MyHandler caught : " + e.Message); 
} 

public static void Main() 
{ 
    Example(); 
} 

}

+0

由於這是我一直在尋找。我必須稍微玩一下,因爲它由於某種原因在Visual Studio中的版本的發佈版本上工作,但是,當它被編譯到一個.exe中時它不起作用。 – 2013-02-08 13:55:32

+1

通過還添加了它的工作:Application.ThreadException + = ProcessThrException; – 2013-02-08 14:08:09

+0

在我鏈接到的頁面上有更多關於可能搶佔這一個的事件的信息 - 你可能需要在你的exe中處理一些其他事件? (ETA - 猜猜我們在同一時間輸入相同的東西:)) – NDJ 2013-02-08 14:09:43

相關問題