我有一个VSTO应用程序,它使用Windows窗体用户控件来托管WPF窗体。 我需要实现一个全局异常处理。 尝试从顶级用户控件处理AppDomain.CurrentDomain.UnhandledException,但在引发异常时永远不会调用该函数。
任何指导在这种情况下,我在哪里/如何实现全局异常处理?
答案 0 :(得分:0)
这是VB.NET中我在项目中使用的示例。我使用一个类进行错误处理。
Public Class ErrorHandler
Public Shared Sub DisplayMessage(ex As Exception)
Dim sf As New System.Diagnostics.StackFrame(1)
Dim caller As System.Reflection.MethodBase = sf.GetMethod()
Dim currentProcedure As String = (caller.Name).Trim()
Dim errorMessageDescription As String = ex.ToString()
Dim msg As String = "Contact your system administrator. " + Environment.NewLine
msg += (Convert.ToString("Procedure: ") & currentProcedure) + Environment.NewLine
msg += "Description: " + ex.ToString() + Environment.NewLine
MessageBox.Show(msg, "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Sub
End Class
Public Class Ribbon
Public Sub YourProcedure()
Try
'try stuff here...
Catch ex As Exception
ErrorHandler.DisplayMessage(ex)
Finally
End Try
End Sub
End Class