通过自定义异常和一个单独的Try ...意外错误处理,在Button回调级别捕获

时间:2019-02-18 16:35:44

标签: excel vb.net vsto

我正在开发Office加载项(Excel + Word + PowerPoint)。 可以将AddIn视为执行相对基本任务的小宏的集合。每个任务大多彼此独立,并通过RibbonUI调用。

由于每个宏都是独立的,所以我决定直接通过Error实现一个非常基本的Ribbon callbacks陷阱:

Private Sub Button_Click()
    Try
        Call DoSomething
    Catch
        MsgBox("Something went wrong!")
    End Try
End Sub

我有一组自定义Exceptions(例如,如果selection range与宏不兼容)。在我的代码中,当我知道错误是来自用户时,我Throw毁了Exceptions。因此,在我的Catch中,我检查了Type的{​​{1}}:

  • 如果它是自定义Exception,则我ExceptionLogEvent,并向用户指示他们没有遵循正确的程序。
  • 如果它是本地Warning,则我ExceptionLogEvent,我邀请用户单击[Report Bug]按钮。此按钮将导出日志并将其发送给我。

我的问题:我看到一些评论,指出应将Custom Exceptions保留给旨在供他人分发和重用的类库。例如,请参见Cody Gray post,他建议始终使用本机异常。 任何人都可以向我解释一下我的代码一旦分发将遇到什么问题?


RibbonUI.vb

Error

Module1.vb

Imports Microsoft.Office.Tools.Ribbon
Imports System.Diagnostics

Public Class RibbonExcel

    Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
    End Sub

    Private Sub Button_GetWeightedAverage_Click(sender As Object, e As RibbonControlEventArgs) Handles Button_GetWeightedAverage.Click
        Try

            Call GetWeightedAverage()
        Catch ex As Exception
            Select Case ex.GetType
                Case GetType(CustomExceptions.WrongSelectionException)
                    EventLog.WriteEntry("Debug",
                                "UI Warning: {ExMessage}".Replace("{ExMessage}", ex.Message) & vbNewLine _
                                & "In: {ExSource}".Replace("{ExSource}", ex.Source) & vbNewLine _
                                & "Trace: {ExStack}".Replace("{ExStack}", ex.StackTrace),
                                EventLogEntryType.Warning)
                    System.Windows.Forms.MessageBox.Show("Something went wrong: {ExMessage}".Replace("{ExMessage}", ex.Message) & vbNewLine _
                                                         & "Read the Help File for further assistance. If the problem remains, please click the [Report Bug] button.",
                                                         "MyApp",
                                                         Windows.Forms.MessageBoxButtons.OK,
                                                         Windows.Forms.MessageBoxIcon.Exclamation,
                                                         Nothing,
                                                         Nothing,
                                                         "MyHelpFGile.hlp", "GetWeightedAverage")
                Case Else
                    EventLog.WriteEntry("Debug",
                                "UI Error: {ExMessage}" & vbNewLine _
                                & "In: {ExSource}" & vbNewLine _
                                & "Trace: {ExStack}".Replace("{ExMessage}", ex.Message).Replace("{ExSource}", ex.Source).Replace("{ExStack}", ex.StackTrace),
                                EventLogEntryType.Error)
                    MsgBox("Something went completely wrong! If the problem remains, please click the [Report Bug] button.", vbCritical Or vbOKOnly)
            End Select
        End Try

    End Sub

End Class

0 个答案:

没有答案