Vbscript 什么是;“错误时继续下一步”;你怎么做?
我看了一些VBScript示例,在脚本的开头看到了关于ErrorResume Next的语句Vbscript 什么是;“错误时继续下一步”;你怎么做?,vbscript,error-handling,Vbscript,Error Handling,我看了一些VBScript示例,在脚本的开头看到了关于ErrorResume Next的语句 它的作用是什么?它基本上告诉程序当遇到错误时,只需在下一行继续执行。当发生错误时,执行将在下一行继续,而不会中断脚本。它的意思是,当该行发生错误时,它告诉vbscript继续执行,而不会中止脚本。有时,On Error跟随Goto标签来改变执行流程,在Sub代码块中是这样的,现在您知道了使用Goto会产生意大利面代码的原因和方式: Sub MySubRoutine() On Error Goto
它的作用是什么?它基本上告诉程序当遇到错误时,只需在下一行继续执行。当发生错误时,执行将在下一行继续,而不会中断脚本。它的意思是,当该行发生错误时,它告诉vbscript继续执行,而不会中止脚本。有时,On Error
跟随Goto
标签来改变执行流程,在Sub
代码块中是这样的,现在您知道了使用Goto
会产生意大利面代码的原因和方式:
Sub MySubRoutine()
On Error Goto ErrorHandler
REM VB code...
REM More VB Code...
Exit_MySubRoutine:
REM Disable the Error Handler!
On Error Goto 0
REM Leave....
Exit Sub
ErrorHandler:
REM Do something about the Error
Goto Exit_MySubRoutine
End Sub
子MySubRoutine()
关于错误转到错误处理程序
REM VB代码。。。
REM更多VB代码。。。
退出我的子程序:
REM禁用错误处理程序!
错误转到0
雷姆离开。。。。
出口接头
错误处理程序:
REM对这个错误做些什么
转到退出我的子程序
端接头
值得注意的是,即使在“错误恢复下一步”
生效时,错误发生时仍然会填充Err对象,因此您仍然可以执行C风格的错误处理
On Error Resume Next
DangerousOperationThatCouldCauseErrors
If Err Then
WScript.StdErr.WriteLine "error " & Err.Number
WScript.Quit 1
End If
On Error GoTo 0
On Error语句-指定当发生运行时错误时,控件将转到紧跟在该语句之后的语句。错误对象的填充方式。(Err.Number、Err.Count等)错误恢复下一步意味着出错时,它将恢复到要恢复的下一行
e、 g.如果您尝试try块,则在发生错误时将停止脚本它将启用错误处理。以下部分内容来自
”启用错误处理。当发生运行时错误时,控件转到语句
'紧接着发生错误的语句,执行
从那一点继续。
出错时继续下一步
这里有人
如果Err.Number=0,则
Echo“SomeCodeHere中没有错误。”
其他的
WScript.Echo“SomeCodeHere中的错误:”&Err.Number&“,“&Err.Source&“,”&Err.Description
'清除错误,否则您将在测试Err.Number时再次看到它
呃,明白了
如果结束
再来点
如果错误号为0,则
WScript.Echo“SomeMoreCodeHere中的错误:”&Err.Number&“,“&Err.Source&“,”&Err.Description
'清除错误,否则您将在测试Err.Number时再次看到它
呃,明白了
如果结束
'在当前过程中禁用已启用的错误处理程序,并将其重置为零。
错误转到0
'还有'On Error Goto-1',它将禁用当前数据库中已启用的异常
'过程并将其重置为零,以及'On Error Goto line',
'启用从中指定的行开始的错误处理例程
'必需的行参数。行参数是任何行标签或行号。如果是运行时
'错误发生时,控件分支到指定行,使错误处理程序处于活动状态。
'指定的行必须与On Error语句位于同一过程中,
'或将发生编译时错误。
这是一种非常强大但危险的语法。使用它要非常小心。现在它更有意义了。在完成一些可能最终出错的函数之后。它们后面有一个名为checkError的函数。是的,现在是2018年,但这仍然有效-在错误恢复下一步时
就像说“我认为这件事应该行得通,但如果不行,就表现得像以前一样”。实际上,这给未来的维护人员带来了很多麻烦。不惜一切代价避免它。VBScript不支持错误转到标签上的
语法,只支持错误转到0上的
。在VBA(Excel 2016)或VBScript中使用Try/catch对我都不起作用。
' Enable error handling. When a run-time error occurs, control goes to the statement
' immediately following the statement where the error occurred, and execution
' continues from that point.
On Error Resume Next
SomeCodeHere
If Err.Number = 0 Then
WScript.Echo "No Error in SomeCodeHere."
Else
WScript.Echo "Error in SomeCodeHere: " & Err.Number & ", " & Err.Source & ", " & Err.Description
' Clear the error or you'll see it again when you test Err.Number
Err.Clear
End If
SomeMoreCodeHere
If Err.Number <> 0 Then
WScript.Echo "Error in SomeMoreCodeHere:" & Err.Number & ", " & Err.Source & ", " & Err.Description
' Clear the error or you'll see it again when you test Err.Number
Err.Clear
End If
' Disables enabled error handler in the current procedure and resets it to Nothing.
On Error Goto 0
' There are also `On Error Goto -1`, which disables the enabled exception in the current
' procedure and resets it to Nothing, and `On Error Goto line`,
' which enables the error-handling routine that starts at the line specified in the
' required line argument. The line argument is any line label or line number. If a run-time
' error occurs, control branches to the specified line, making the error handler active.
' The specified line must be in the same procedure as the On Error statement,
' or a compile-time error will occur.