抑制整个脚本的错误

Jon*_*oux 6 error-handling vbscript gpo

我想要抑制可能出现在我的VBS登录脚本中的所有错误.

我可以用以下内容包围WHOLE 500行脚本:

On Error Resume Next

'[... whole script (~500 lines of code) ...]

On Error GoTo 0
Run Code Online (Sandbox Code Playgroud)

Ekk*_*ner 9

可以这样做 - 即使没有OEG0线 - 但是你不应该这样做,因为脚本将继续执行第i行......最后,即使第i-1行中的错误使你关于必要的前提条件的所有假设无效那些行中的行动.您的策略可与睁着眼睛的驾驶相媲美,以避免被其他车辆的前灯弄得眼花缭乱.

如果您无法对选定的操作执行本地重新排序的错误处理 -

...
On Error Resume Next
  risky_action
  save Err
On Error GoTo 0
If ErrorOccurred Then
   something sensible
   If can't continue Then
      WScript.Quit 4711
   End If
End If
...
Run Code Online (Sandbox Code Playgroud)

试着逃避

Sub Main()
  ... you 500 lines ...
End Sub 

On Error Resume Next
  Main
  If Err.Number Then
     WScript.Echo "aborted"
     WScript.Quit 4711
  End If
Run Code Online (Sandbox Code Playgroud)

此方法可确保不会执行错误后的行.

  • 你是VBS的神吗? (3认同)
  • @JonathanRioux - 不,因为那时我会创建VBScript,以便所有类型的错误都是不可能的. (2认同)