VBA 代码中的错误处理
Visual Basic 使用
Err
对象作为处理错误的标准机制。当
FactoryTalk View SE
Client 对象模型遇到错误时,将使用此标准 Visual Basic 机制生成错误。如果不提供错误处理,VBA 或 VBA 的运行时错误处理程序将向最终用户显示运行时错误。始终
应避免发生这种情况,因为它会导致过程意外终止。以下代码示例给出了没有错误处理程序的子程序遇到错误时发生的情况。当子程序代码尝试为集合中不存在的条目设置变量时,VBA 将检测到错误。其结果是出现一个默认的运行时错误对话框。此对话框提供的操作可以停止子程序和复位 VBA 工程。应始终避免这种情况。
Sub ExampleNoErrorHandling() Dim oDisplay As DisplayClient.Display ' Set the display object to a display that is not loaded. Set oDisplay = Application.LoadedDisplays("Unknown Display") ' The code will stop running on the line above, and will produce a run-time error dialog in FactoryTalk View Studio, ' or will log the error condition in the FactoryTalk View SE Client. ' The next line will not be run. oDisplay.SetFocus End Sub
提供反馈