2010-02-04 15 views
49

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?

+6

È un bit di sintassi molto potente, ma pericoloso. Sii molto cauto usandolo. – Nate

+2

Ha più senso ora. Dopo alcune funzioni che possono finire per errore. Hanno una funzione chiamata checkError dopo di loro. – Omar

risposta

65

Fondamentalmente dice al programma che quando si verifica un errore, continuare alla riga successiva.

21

Quando si verifica un errore, l'esecuzione continuerà sulla riga successiva senza interrompere lo script.

9

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 
+10

VBScript non supporta la sintassi 'Su errore Goto Label', solo' Su errore Goto 0'. – Helen

32

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 
3

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.)

0

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

+0

Utilizzo Try/catch non funziona per me in VBA (Excel 2016) né VBScript. – user2415376

0

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.