本文介绍了VBScript - 使用错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用VBScript来捕获错误并记录它们(例如错误登录某些东西),然后恢复脚本的下一行。

I want to use VBScript to catch errors and log them (ie on error "log something") then resume the next line of the script.

例如,


On Error Resume Next
'Do Step 1
'Do Step 2
'Do Step 3

当步骤1发生错误时,我希望它记录该错误(或使用它执行其他自定义功能),然后在步骤这是可能吗?如何实现?

When an error occurs on step 1, I want it to log that error (or perform other custom functions with it) then resume at step 2. Is this possible? and how can I implement it?

编辑:我可以这样做吗?

Can I do something like this?


On Error Resume myErrCatch
'Do step 1
'Do step 2
'Do step 3

myErrCatch:
'log error
Resume Next


推荐答案

VBScript没有抛出或捕获异常的概念,但运行时提供一个全局Err对象,其中包含执行的最后一个操作的结果。您必须明确检查每次操作后Err.Number属性是否为非零。

VBScript has no notion of throwing or catching exceptions, but the runtime provides a global Err object that contains the results of the last operation performed. You have to explicitly check whether the Err.Number property is non-zero after each operation.

On Error Resume Next

DoStep1

If Err.Number <> 0 Then
  WScript.Echo "Error in DoStep1: " & Err.Description
  Err.Clear
End If

DoStep2

If Err.Number <> 0 Then
  WScript.Echo "Error in DoStop2:" & Err.Description
  Err.Clear
End If

'If you no longer want to continue following an error after that block's completed,
'call this.
On Error Goto 0

On Error Goto [label]语法支持Visual Basic和Visual Basic应用程序(VBA),但VBScript不支持此语言功能,因此您必须使用上述错误恢复下一步。

The "On Error Goto [label]" syntax is supported by Visual Basic and Visual Basic for Applications (VBA), but VBScript doesn't support this language feature so you have to use On Error Resume Next as described above.

这篇关于VBScript - 使用错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 10:00