Encontré algunos ejemplos de VBScript, y vi la declaración On Error Resume Next
básicamente al comienzo de la secuencia de comandos.¿Qué significa la instrucción "On Error Resume Next"?
¿Qué hace?
Encontré algunos ejemplos de VBScript, y vi la declaración On Error Resume Next
básicamente al comienzo de la secuencia de comandos.¿Qué significa la instrucción "On Error Resume Next"?
¿Qué hace?
Básicamente le dice al programa que cuando encuentre un error simplemente continúe en la siguiente línea.
Cuando se produce un error, la ejecución continuará en la siguiente línea sin interrumpir el script.
Significa que, cuando ocurre un error en la línea, le indica a vbscript que continúe la ejecución sin abortar el script. A veces, el On Error
sigue la etiqueta de Goto
para alterar el flujo de ejecución, algo como esto en un bloque Sub
código, ya sabes por qué y cómo el uso de GOTO
puede resultar en código espagueti:
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 no admite la sintaxis 'On Error Goto Label', solo' On Error Goto 0'. – Helen
Vale la pena señalar que incluso cuando está en vigor On Error Resume Next
, el objeto Err aún se llena cuando se produce un error, por lo que aún puede realizar el manejo de errores al estilo C.
On Error Resume Next
DangerousOperationThatCouldCauseErrors
If Err Then
WScript.StdErr.WriteLine "error " & Err.Number
WScript.Quit 1
End If
On Error GoTo 0
Instrucción On Error - Especifica que cuando se produce un error en tiempo de ejecución, el control pasa a la instrucción inmediatamente después de la declaración. ¿Cómo se llenó el objeto Err? (Números Err., Err.Count, etc.)
Currículum de error Next significa que, en caso de error, se reanudará a la siguiente línea para reanudar.
p. Ej. si prueba el bloque Try, eso detendrá el script si se produjo un error
Usar Try/catch no funciona para mí en VBA (Excel 2016) ni en VBScript. – user2415376
Permite el manejo de errores. Lo siguiente es en parte de 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.
Es una sintaxis muy potente pero peligrosa. Ten mucho cuidado al usarlo. – Nate
Tiene más sentido ahora. Después de algunas funciones que pueden terminar en error. Tienen una función llamada checkError después de ellos. – Omar