Sono arrivato ad alcuni esempi VBScript e ho visto la dichiarazione On Error Resume Next
fondamentalmente all'inizio dello script.Cosa fa l'istruzione "On Error Resume Next"?
Cosa fa?
Sono arrivato ad alcuni esempi VBScript e ho visto la dichiarazione On Error Resume Next
fondamentalmente all'inizio dello script.Cosa fa l'istruzione "On Error Resume Next"?
Cosa fa?
Fondamentalmente dice al programma che quando si verifica un errore, continuare alla riga successiva.
Quando si verifica un errore, l'esecuzione continuerà sulla riga successiva senza interrompere lo script.
Significa che quando si verifica un errore sulla linea, sta dicendo a vbscript di continuare l'esecuzione senza interrompere lo script. A volte, il On Error
segue l'etichetta Goto
di alterare il flusso di esecuzione, qualcosa di simile in un blocco Sub
codice, ora sapete perché e come l'utilizzo di GOTO
può provocare spaghetti code:
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
VBScript non supporta la sintassi 'Su errore Goto Label', solo' Su errore Goto 0'. – Helen
Vale la pena notare anche quando On Error Resume Next
è attivo, l'oggetto Err viene ancora popolato quando si verifica un errore, quindi è ancora possibile eseguire la gestione degli errori in stile C.
On Error Resume Next
DangerousOperationThatCouldCauseErrors
If Err Then
WScript.StdErr.WriteLine "error " & Err.Number
WScript.Quit 1
End If
On Error GoTo 0
Istruzione On Error - Specifica che quando si verifica un errore di run-time, il controllo passa alla dichiarazione subito dopo la dichiarazione. Come mai l'oggetto Err è stato popolato. (Err.Number, Err.Count ecc.)
In Errore Riprendi Avanti significa che in caso di errore, riprenderà la riga successiva per riprendere.
ad es. se provi il blocco Try, questo interromperà lo script se si è verificato un errore
Utilizzo Try/catch non funziona per me in VBA (Excel 2016) né VBScript. – user2415376
Abilita la gestione degli errori. Quanto segue è in parte da 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.
È un bit di sintassi molto potente, ma pericoloso. Sii molto cauto usandolo. – Nate
Ha più senso ora. Dopo alcune funzioni che possono finire per errore. Hanno una funzione chiamata checkError dopo di loro. – Omar