示例:检测子程序中的错误
以下示例代码所示为如何检测子程序中的错误。发生错误时,
Err
对象将记录错误信息,子程序将继续执行下一语句。本示例表明,错误由对象的 Number
属性确定,且代码执行路径基于此值。如果在运行代码并出现错误之后没有执行错误返回检查,则将继续执行代码。在某些情况下,可以忽略产生的错误,但却可能因为忽略的错误导致出现其他无法忽略的严重错误而产生意外的结果。
Sub ExampleInLineErrorHandling() Dim oDisplay As DisplayClient.Display ' Set the Error handler to jump to the next line on any error. On Error Resume Next Err.Clear ' Set the display object to a display that is not loaded. Set oDisplay = Application.LoadedDisplays("Unknown Display") Select Case Err.Number Case 0: oDisplay.SetFocus Case gfxErrorConstants.gfxErrorCollectionIndex: Application.LogDiagnosticsMessage "ExampleInLineErrorHandling(): Unable to load display 'Unknown Display'", _ ftDiagSeverityError Case Else Application.LogDiagnosticsMessage "ExampleInLineErrorHandling(): Error # " _ & Err.Number & "," & Err.Description, _ ftDiagSeverityError End Select End Sub
提供反馈