开发者

Err.clear in vbscript

in this vbscript the second Err.Clear is required or not as a part of optimization.

   Set WshShell = CreateObject("WScript.Shell")
   strSpoRootKey = "HKLM\SOFTWARE\"
   strKey = RootKey & "tcc\Operation\" & Version & "\Location"
   strPath = wshShell.RegRead(strKey)
   If Err Then
      Err.Clear  //first Err.clear
       RootKey = "HKLM\SOFTWARE\Wow6432Node"
       strKey = RootKey & "tcc\Operation\" & Version & "\Location"
       strPath = wshShell.RegRead(strKey)
       If Err Then
           Err.Clear   // second Err.clear
           strEr开发者_开发技巧r = strErr & ": " & Version & ": " & strKey & ":error("  
       End If
   End If

Shall we remove the second Err.Clear from code


The purpose of calling Err.Clear to explicitly clear the Err object after you have trapped and handled an error. MSDN suggests that this is especially useful when you are using deferred error handling with On Error Resume Next. This suggests that you really should be calling Err.Clear after you have handled the error that was detected, whatever that means for your particular application.

You didn't really provide enough of your code to suggest the kind of error handing routines that you're using. For example, if you have On Error Resume Next in your code above the block that you posted, and/or what you're doing with strErr to actually handle the error. Does your script end at the end of the posted section? If so, you're probably in the clear removing the second Err.Clear, since it'll be impossible for another error be raised anyway during the script's course of execution.

Regardless, I'm going to take the easy way out and suggest that you do the same: there is absolutely no reason to "optimize" this code by removing any Err.Clear statements. I promise that statement is not bottlenecking the execution speed of your script. Unless you are experiencing problems with your error handling routines (like trapping the same error twice or failing to trap an error because it has been cleared prematurely), there's little reason to worry about "extra" Err.Clear statements.


yes second Err.Clear should be downside once error handled properly other wise it carry forward to next stage

so things will change to

If Err Then           
       strErr = strErr & ": " & Version & ": " & strKey & ":error("  
       Err.Clear   // second Err.clear
End If
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜