VBScript not exiting out of 'Do-While' loop?
Since VBScrip开发者_开发知识库t has notoriously bad built-in error handling, I've tried getting around that by wrapping my main blocks in a Do While Err.Number = 0
loop. Theoretically, this should work: if a script has On Error Resume Next
statement enabled, the script will bypass the built-in error handler in WSH (i.e. stop it completely) while still populating its Err
object. What should happen next is that the error number changes, which should trigger an interruption in the loop and have it jump out to my other branch of code. (Sorry for the ASM references.)
However, this doesn't seem to work all of the time. Sometimes, it will do exactly what's described above and work beautifully. Other times, though, it just completely ignores the change and keeps going. Even when I check the object in a debugger, I can see WSH changing the error number and its other members but blindly ignoring the loop condition!
What gives?
Example:
Dim TestObject
Do While Err.Number = 0
Set TestObject = CreateObject("Scripting.FileSystem") 'should trigger an error
WScript.Echo "Who cares about your DO?!"
Loop
WScript.Echo "Script should go here."
WScript.Quit(0)
Thanks!
Actually, never mind. VBscript isn't like compiled languages and can't branch out of loops because it executes every statement procedurally. Yet another reason to switch my scripts to .NET or something.
精彩评论