Я пришел к некоторым примерам VBScript, и я увидел инструкцию On Error Resume Next
в основном в начале script.
Что он делает?
Я пришел к некоторым примерам VBScript, и я увидел инструкцию On Error Resume Next
в основном в начале script.
Что он делает?
В основном он говорит программе, когда вы сталкиваетесь с ошибкой, просто продолжайте на следующей строке.
Стоит отметить, что даже когда On Error Resume Next
действует, объект 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
При возникновении ошибки выполнение будет продолжено на следующей строке без прерывания script.
Это означает, что когда ошибка возникает в строке, она сообщает vbscript продолжить выполнение без прерывания script. Иногда 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
В сообщении об ошибке - указывает, что при возникновении ошибки времени выполнения управление переходит к следующему оператору. Как только объект Err заселен (Err.Number, Err.Count и т.д.)
В поле "Ошибка при повторном включении" означает, что "При ошибке" оно возобновится до следующей строки.
например. если вы попробуете блок Try, это остановит script, если произошла ошибка
Он позволяет обрабатывать ошибки. Ниже отчасти https://msdn.microsoft.com/en-us/library/5hsw66as.aspx
' 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.