2012-02-04 39 views
1

IM發展與錯誤日誌中的應用程序通過發送郵件的錯誤,當事情變壞時,必須通過郵件錯誤的詳細信息發送這樣我就可以遠程修復和上傳與修復新的更新。VB.Net當檢測到錯誤

進出口使用Try Catch Exception,但我有很多的方法,包括在此選項中,有另一種方式做到這一點沒有做那麼多的代碼?

感謝,並有一個愉快的週末!

回答

2

由於異常氣泡到應用程序實例試圖使用Application.SetUnhandledExceptionMode方法。

從MSDN上面的鏈接:

它往往是不可行捕捉所有通過 Windows窗體拋出的異常。使用這種方法,您可以指示您的應用程序 是否應該抓住由Windows 窗體組件拋出所有未處理的異常,並繼續工作,或者是否應公開 他們給用戶,並中止執行,。

Public Shared Sub Main() 
    ' Add the event handler for handling UI thread exceptions to the event. 
    AddHandler Application.ThreadException, AddressOf Form1_UIThreadException 

    ' Set the unhandled exception mode to force all Windows Forms errors to go through 
    ' our handler. 
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException) 

    ' Add the event handler for handling non-UI thread exceptions to the event. 
    AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf CurrentDomain_UnhandledException 

    ' Runs the application. 
    Application.Run(New Form1()) '' This is your applications Main Form 
End Sub 

Private Shared Sub Form1_UIThreadException(ByVal sender As Object, ByVal t As ThreadExceptionEventArgs) 

'Put Error Handling Code here see the MSDN article for an example implementation 

End Sub 

Private Shared Sub CurrentDomain_UnhandledException(ByVal sender As Object, _ 
    ByVal e As UnhandledExceptionEventArgs) 

''Put Error Handling Code here see the MSDN article for an example implementation 

End Sub 
+0

這個例子不工作,ErrorHandlerForm沒有發現...什麼即時通訊做錯了什麼? – 2012-02-04 02:35:09

+0

你有沒有去鏈接,看看那裏的完整代碼?它是在那裏聲明的Application.ThreadException的事件處理程序。他們的名字也被包括的主要形式 – 2012-02-04 02:57:29

+0

名研究的鏈接了一段時間我做它的工作後!非常感謝! – 2012-02-04 03:47:03

1

對不起,誤解了你的問題。試着把你的邏輯放在一個方法中,並試着在你所有的try catch語句中調用該方法。

實施例:

Public Shared Sub Method1() 

    Try 
    'Method logic here 
    Catch ex As Exception 
     EmailError(ex) 
    End Try 

End Sub 

Public Shared Sub EmailError(ex As Exception) 

    'your remote error email logic here 

End Sub