I've been trying to goto a specific sub when an error is detected, but everything other then on error resume next seems to cause a compile error, any idea why?
-John
VB error handling?
Moderators: Dorian (MJT support), JRL
-
- Junior Coder
- Posts: 24
- Joined: Tue Sep 15, 2009 3:20 pm
VB error handling?
I think I can automate that!
See this link for more information -- a small excerpt follows:
http://technet.microsoft.com/en-us/libr ... 92852.aspx
http://technet.microsoft.com/en-us/libr ... 92852.aspx
Handling Errors with VBScript
This column being part of the "Doctor Scripto's Script Shop" series, we're going to go out on a limb and assume you've already read the Windows 2000 Scripting Guide sections on error handling, or at least have some experience catching errors.
Just to jog your memory, though, let’s do a quick review. VBScript error-handling requires two elements that work together. You can turn it on with the On Error Resume Next statement and turn it off with On Error GoTo 0. When it's turned on you can use the built-in Err object to get some information on what kind of error occurred.
Before you can check for an error, you have to include the statement On Error Resume Next. If you check the Err object without first turning on error handling with On Error Resume Next, VBScript assumes that Err.Number is 0; in other words, that no error has occurred. The script will then continue to do whatever comes next, assuming that all is well. If an error has in fact occurred, it may cause the script to fail with an unhandled run-time error that brings everything grinding to a halt.
Putting On Error Resume Next at the beginning of the script, as we often do, makes it apply to the entire body of the script. But, as we'll see in later examples, its scope does not include functions or subroutines. If you want to handle errors within a function or subroutine, you must also include On Error Resume Next in each of them before checking the Err object.
You can turn error-handling off with On Error GoTo 0. So it's possible to turn error-handling on with On Error Resume Next just before you want to check the Err object, and turn it off after with On Error GoTo 0. This makes more explicit exactly where errors are being handled, but to the jaded eyes of the Scripting Guys it seems like a lot of work for minimal returns in most cases.
On Error Resume Next can hide syntax errors, but you can avoid that problem by commenting out On Error Resume Next when debugging the script:
-
- Junior Coder
- Posts: 24
- Joined: Tue Sep 15, 2009 3:20 pm
Dang
Thanks for the additional link, this confirms what I had assumed.
Scripting only supports the on error resume next syntax.
I was hoping for an on error goto errorhandling or something like that.
Scripting only supports the on error resume next syntax.
I was hoping for an on error goto errorhandling or something like that.
I think I can automate that!