Ik kwam tot enkele VBScript-voorbeelden en ik zag de instructie On Error Resume Next
eigenlijk aan het begin van het script.
Wat doet het?
Antwoord 1, autoriteit 100%
Het vertelt het programma in feite wanneer je een fout tegenkomt, ga gewoon verder op de volgende regel.
Antwoord 2, autoriteit 52%
Het is vermeldenswaard dat zelfs wanneer On Error Resume Next
van kracht is, het Err-object nog steeds wordt ingevuld wanneer er een fout optreedt, zodat u nog steeds C-stijl foutafhandeling kunt uitvoeren.
On Error Resume Next
DangerousOperationThatCouldCauseErrors
If Err Then
WScript.StdErr.WriteLine "error " & Err.Number
WScript.Quit 1
End If
On Error GoTo 0
Antwoord 3, autoriteit 28%
Als er een fout optreedt, gaat de uitvoering verder op de volgende regel zonder het script te onderbreken.
Antwoord 4, autoriteit 14%
Het betekent dat wanneer er een fout optreedt op de regel, het vbscript vertelt om door te gaan met de uitvoering zonder het script af te breken. Soms volgt de On Error
het GOTO
label om de uitvoeringsstroom te wijzigen, zoiets als dit in een Sub
codeblok, nu weet je waarom en hoe het gebruik van GOTO
kan resulteren in spaghetticode:
Sub MijnSubRoutine() Bij fout Ga naar ErrorHandler REM VB-code... REM Meer VB-code... Exit_MySubRoutine: REM Schakel de foutafhandelaar uit! Bij fout Ga naar 0 REM Verlof.... Sluit sub ErrorHandler: REM Doe iets aan de fout Ga naar Exit_MySubRoutine Einde sub
Antwoord 5, autoriteit 3%
On Error Statement – Geeft aan dat wanneer er een runtime-fout optreedt, de besturing naar de instructie gaat die direct volgt op de instructie. Hoe ooit het Err-object is ingevuld.(Err.Number, Err.Count enz.)
Antwoord 6, autoriteit 3%
Het maakt foutafhandeling mogelijk. Het volgende is gedeeltelijk afkomstig van https://msdn.microsoft.com/en-us /bibliotheek/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.
Antwoord 7
Bij fout hervatten volgende betekent dat bij fout wordt hervat naar de volgende regel om te hervatten.
bijv. als je het Try-blok probeert, zal dat het script stoppen als er een fout is opgetreden