示例:子程序中简单的错误处理
下面的代码示例显示了一个子程序的简单错误处理程序。发生错误时,代码将跳转至
ErrHandler:
标记,并将错误记录到 FactoryTalk Diagnostics 中,代码会从导致错误的行后面的行继续执行。本示例将始终在出现错误时发出报告,但是不会报告哪一行导致此错误。Sub ExampleSimpleErrorHandling() ' Set the Error handler to jump to the ErrHandler: label on any error condition. On Error GoTo ErrHandler Dim oDisplay As DisplayClient.Display ' Set the display object to a display that is not loaded. Set oDisplay = Application.LoadedDisplays("Unknown Display") ' code will continue to run because the Resume Next statement was called, ' but will also have an error since the variable oDisplay was not set. oDisplay.SetFocus Exit Sub ErrHandler: ' Simply log the error message and continue Application.LogDiagMessage "Error occurred in ExampleSimpleErrorHandling()." & _ "Error# " & Err.Number & ", " & Err.Description, _ ftDiagSeverityError Resume Next End Sub
提供反馈