Announcement

Collapse
No announcement yet.

How does the "On Error Resume Next" behave?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How does the "On Error Resume Next" behave?

    Right now I'm going through the Windows 2000 Scripting Guide so I'm still learning.

    I have a script that I wrote (borrowing some code from Rems ) that works when I state On Error Resume Next just before the lines that need it. But if I put it at the beginning of the script it seems to exit the subroutine that the error occurs in.

    Is that how it's supposed to work?

    Here's the version that works

    Code:
    Option Explicit
    'On Error Resume Next
    
    
    Dim intMappedDrv, intLoopA, intLoopB, intTime
    Dim strIPGateway, strComputer, strUsername, strPassword, preDrive 
    Dim sFolderShare, sAlias, strDrvLtr, strSharePath, strDisplayName, strConfGate
    Dim FreeDrive
    Dim objWMIService, objNet, colNetCons, objNetCon, colNICSet, objNICSet, fso
    Dim oShell, objNetDrv
    
    strIPGateway = "172.19.12.129"
    strDrvLtr = "P:"
    strSharePath = "\\server\share"
    strDisplayName = "Authenticator"
    strComputer = "."
    intLoopA = 0
    intLoopB = 0
    intTime = 30000 '<--------equal to 30 seconds
    
    Do Until intLoopA = 11
    
       Checker
       Wscript.Sleep(intTime)
    
    Loop
    
    
    Sub Checker
       Set objNet = CreateObject("Wscript.Network")
       Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    
       Do
          Set colNetCons = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkConnection" & _
                                            " WHERE RemotePath='\\\\server\\share'")
          Set colNICSet = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration")
          intLoopB = 0
          If colNetCons.Count = 0 Then
             For Each objNICSet in colNICSet
                On Error Resume Next
                strConfGate = Join(objNICSet.DefaultIPGateway,",")
                On Error GoTo 0
                If strConfGate = strIPGateway Then
                   strConfGate = ""
                   MapDrive strDrvLtr,strSharePath,strDisplayName
                   Exit Sub
                Else
                   strConfGate = ""
                End If
             Next
          Else
             For Each objNetCon In colNetCons
                If NOT objNetCon.Status = "OK" Then
                   objNet.RemoveNetworkDrive objNetCon.LocalName,True,True
                   intLoopB = 1
                End If
             Next
          End If
       Loop Until intLoopB = 0
    End Sub
    
    Sub MapDrive(preDrive,sFolderShare,sAlias)
       Set objNet = WScript.CreateObject("WScript.Network")
       Set fso = CreateObject("Scripting.FileSystemObject")
       Set oShell = CreateObject("Shell.Application")
       Set objNetDrv = objNet.EnumNetworkDrives
    
    	
       '**********************************************************
       '* Checks to see if the "strDrvLtr" is in use. 
       '* map to the next available drive letter on "Else"
       '**********************************************************
       If Not fso.DriveExists(preDrive) Then
          FreeDrive = preDrive
       Else
          For i = Asc("D") To Asc("Z") 
             If Not fso.DriveExists(Chr(i)) Then 
                FreeDrive = Chr(i) & ":" 
                Exit For 
             End If 
          Next
       End If
        
       '*****************************************
       '* Maps the drive and sets the description
       '*****************************************
       GetUserPass
    
       objNet.MapNetworkDrive FreeDrive, sFolderShare, False, strUserName, strPassword
       oShell.NameSpace(FreeDrive).Self.Name = sAlias
    
       Set oShell = Nothing
       FreeDrive = preDrive
    End Sub
    
    
    '******************************
    'Inserted by the Install Script
    '******************************
    Sub GetUserPass
       strUsername = "username"
       strPassword = "password"
    End Sub

    Here's the version that doesn't

    Code:
    Option Explicit
    On Error Resume Next
    
    
    Dim intMappedDrv, intLoopA, intLoopB, intTime
    Dim strIPGateway, strComputer, strUsername, strPassword, preDrive 
    Dim sFolderShare, sAlias, strDrvLtr, strSharePath, strDisplayName, strConfGate
    Dim FreeDrive
    Dim objWMIService, objNet, colNetCons, objNetCon, colNICSet, objNICSet, fso
    Dim oShell, objNetDrv
    
    strIPGateway = "172.19.12.129"
    strDrvLtr = "P:"
    strSharePath = "\\server\share"
    strDisplayName = "Authenticator"
    strComputer = "."
    intLoopA = 0
    intLoopB = 0
    intTime = 30000 '<--------equal to 30 seconds
    
    Do Until intLoopA = 11
    
       Checker
       Wscript.Sleep(intTime)
    
    Loop
    
    
    Sub Checker
       Set objNet = CreateObject("Wscript.Network")
       Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    
       Do
          Set colNetCons = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkConnection" & _
                                            " WHERE RemotePath='\\\\server\\share'")
          Set colNICSet = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration")
          intLoopB = 0
          If colNetCons.Count = 0 Then
             For Each objNICSet in colNICSet
                'On Error Resume Next
                strConfGate = Join(objNICSet.DefaultIPGateway,",")
                'On Error GoTo 0
                If strConfGate = strIPGateway Then
                   strConfGate = ""
                   MapDrive strDrvLtr,strSharePath,strDisplayName
                   Exit Sub
                Else
                   strConfGate = ""
                End If
             Next
          Else
             For Each objNetCon In colNetCons
                If NOT objNetCon.Status = "OK" Then
                   objNet.RemoveNetworkDrive objNetCon.LocalName,True,True
                   intLoopB = 1
                End If
             Next
          End If
       Loop Until intLoopB = 0
    End Sub
    
    Sub MapDrive(preDrive,sFolderShare,sAlias)
       Set objNet = WScript.CreateObject("WScript.Network")
       Set fso = CreateObject("Scripting.FileSystemObject")
       Set oShell = CreateObject("Shell.Application")
       Set objNetDrv = objNet.EnumNetworkDrives
    
    	
       '**********************************************************
       '* Checks to see if the "strDrvLtr" is in use. 
       '* map to the next available drive letter on "Else"
       '**********************************************************
       If Not fso.DriveExists(preDrive) Then
          FreeDrive = preDrive
       Else
          For i = Asc("D") To Asc("Z") 
             If Not fso.DriveExists(Chr(i)) Then 
                FreeDrive = Chr(i) & ":" 
                Exit For 
             End If 
          Next
       End If
        
       '*****************************************
       '* Maps the drive and sets the description
       '*****************************************
       GetUserPass
    
       objNet.MapNetworkDrive FreeDrive, sFolderShare, False, strUserName, strPassword
       oShell.NameSpace(FreeDrive).Self.Name = sAlias
    
       Set oShell = Nothing
       FreeDrive = preDrive
    End Sub
    
    
    '******************************
    'Inserted by the Install Script
    '******************************
    Sub GetUserPass
       strUsername = "username"
       strPassword = "password"
    End Sub
    Regards,
    Jeremy

    Network Consultant/Engineer
    Baltimore - Washington area and beyond
    www.gma-cpa.com

  • #2
    Re: How does the &quot;On Error Resume Next&quot; behave?

    "on error resume next" does exactly what it says -- past any fatal errors, code keeps running.

    Now what you need to understand is the Err object.

    Look at the Err object and perform an Err.Clear after trapping an error.
    Cheers,

    Rick

    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

    2006-2099 R Valstar. This post is offered "as is" for discussion purposes only with no express or implied warranty of any kind including, but not limited to, correctness or fitness for use. Nothing herein shall be construed as advice. Attempting any activity based on information in this post is done at your own risk.

    Comment


    • #3
      Re: How does the &quot;On Error Resume Next&quot; behave?

      Thanks for the reply, Rick.
      I think I have a fairly good grasp on trapping but I've really only been at it for about two weeks.

      Anyways, my question was about the behaviour of On Error Resume Next in relation to Subroutines.

      Here's a script to demonstrate what I'm talking about
      Code:
      On Error Resume Next
      
      set objFakeA = CreateObject("not.real")
      
      Wscript.Echo Err
      Wscript.Echo "An error occurred but we're still going"
      Err.Clear
      
      ThingOne
      Wscript.Echo "After ThingOne has run"
      Wscript.Echo err
      
      
      Wscript.quit
      
      
      Sub ThingOne
      Wscript.Echo "Before Error in SubA"
      set objFakeB = CreateObject("not.real")
      Wscript.Echo Err
      Wscript.Echo "Subroutine Echo"
      Err.clear
      End Sub
      When I run it on my machine, it exits the subroutine before running all the code highlighted in red. But if you add On Error Resume Next at the beginning of the subroutine then it runs as expected.
      Regards,
      Jeremy

      Network Consultant/Engineer
      Baltimore - Washington area and beyond
      www.gma-cpa.com

      Comment


      • #4
        Re: How does the &quot;On Error Resume Next&quot; behave?

        I'm pretty darn sure it applies only within the current context so not having an "on error" statement in a subprogram causes it to error up to the parent where the "on error resume next" kicks in.

        You can search the web but I'm pretty darn sure that the "on error" statement is context specific meaning you need to put one in each subprogram should you wish to control the behavior there.
        Cheers,

        Rick

        ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

        2006-2099 R Valstar. This post is offered "as is" for discussion purposes only with no express or implied warranty of any kind including, but not limited to, correctness or fitness for use. Nothing herein shall be construed as advice. Attempting any activity based on information in this post is done at your own risk.

        Comment


        • #5
          Re: How does the &quot;On Error Resume Next&quot; behave?

          Originally posted by rvalstar View Post
          I'm pretty darn sure it applies only within the current context so not having an "on error" statement in a subprogram causes it to error up to the parent where the "on error resume next" kicks in.

          You can search the web but I'm pretty darn sure that the "on error" statement is context specific meaning you need to put one in each subprogram should you wish to control the behavior there.
          Thanks Rick.
          I guess I should have searched once I suspected the On Error statement to be the issue.

          Here's an excerpt from MSDN http://msdn2.microsoft.com/en-us/library/53f3k80h.aspx
          Within any particular procedure, an error is not necessarily fatal as long as error-handling is enabled somewhere along the call stack. If local error-handling is not enabled in a procedure and an error occurs, control is passed back through the call stack until a procedure with error-handling enabled is found and the error is handled at that point. If no procedure in the call stack is found to have error-handling enabled, an error message is displayed at that point and execution stops or the host handles the error as appropriate.
          Regards,
          Jeremy

          Network Consultant/Engineer
          Baltimore - Washington area and beyond
          www.gma-cpa.com

          Comment


          • #6
            Re: How does the &quot;On Error Resume Next&quot; behave?

            That's interesting - I didn't realize that
            This is my signature.

            Comment


            • #7
              Re: How does the &quot;On Error Resume Next&quot; behave?

              Neither did I.

              I also didn't realize until later that I could have used
              Code:
              Err.Raise 2 '<--- this can be whatever number you want
              Instead of trying to create an nonexistent object
              Code:
              set objFakeA = CreateObject("not.real")
              I learn something new everyday (sometimes quit a bit more than one thing )
              Regards,
              Jeremy

              Network Consultant/Engineer
              Baltimore - Washington area and beyond
              www.gma-cpa.com

              Comment

              Working...
              X