VB error handling?

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
wjfinniganjds
Junior Coder
Posts: 24
Joined: Tue Sep 15, 2009 3:20 pm

VB error handling?

Post by wjfinniganjds » Thu Jun 24, 2010 4:58 pm

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
I think I can automate that!

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

Post by adroege » Thu Jun 24, 2010 5:09 pm

See this link for more information -- a small excerpt follows:

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:

wjfinniganjds
Junior Coder
Posts: 24
Joined: Tue Sep 15, 2009 3:20 pm

Dang

Post by wjfinniganjds » Thu Jun 24, 2010 5:17 pm

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.
I think I can automate that!

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts