What does the “On Error Resume Next” statement do?

后端 未结 7 2569
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 07:54

I came to some VBScript examples, and I saw the statement On Error Resume Next basically at the beginning of the script.

What does it do?

7条回答
  •  死守一世寂寞
    2020-11-29 08:23

    It enables error handling. The following is partly from 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.
    

提交回复
热议问题