我正在开发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
,则我Exception
为LogEvent
,并向用户指示他们没有遵循正确的程序。Warning
,则我Exception
为LogEvent
,我邀请用户单击[Report Bug]按钮。此按钮将导出日志并将其发送给我。我的问题:我看到一些评论,指出应将Custom Exceptions保留给旨在供他人分发和重用的类库。例如,请参见Cody Gray post,他建议始终使用本机异常。 任何人都可以向我解释一下我的代码一旦分发将遇到什么问题?
Error
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