我有一个VSTO应用程序,它使用Windows窗体用户控件来承载WPF窗体。我需要实现一个全局异常处理。尝试从顶级用户控件处理AppDomain.CurrentDomain.UnhandledException,但在引发异常时从不调用该函数。
在这种情况下,我可以在哪里/如何实现全局异常处理?
发布于 2018-08-14 14:30:18
下面是我在项目中使用的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 Classhttps://stackoverflow.com/questions/48504639
复制相似问题