Re: Thoughts on current error handling best practice with VBScript



FUBARinSFO schrieb:
Folks:

These are quite thoughtful submissions. My comments here will not do
them justice, I'm afraid.

Far from it!

1. Powershell (Monad) (from Anthony Jones)

[...]

2. Defensive programming

Anthony Jones also recommends not using VBS if you're concerned about
error handling. I was afraid of that. I always use Option Explicit,
and do try to wrap possible exceptions in some sort of error-handling
code. But even using try-catch is a stretch for some of us, so if I
am in any way representative of the casual programmer it is a constant
struggle to set up your code to handle exceptions On Error ... is
something I used to use, but found it bothersome. That is my
mistake. I used to program from templates that I called up with pre-
formed structure, but have fallen off the wagon on this. All of these
comments make me think that if I'm serious about this I should
reactivate my templates and have another go at that method.

In my opinion, in VBScript you should concentrate on defensive programming,
that is checking the conditions you rely on carefully with normal/standard
code. OERN should be reserved for a few special cases where gaps of design/
implementation

Function RegKeyExists( oShell, sKey )
Dim sVal
On Error Resume Next
sVal = oShell.RegRead( sKey )
RegKeyExists = (0 = Err.Number)
On Error Goto 0
End Function

quirks of the OS

Sub recurseDir( oFolder, ... )
WScript.Echo oFolder.Path ' e.g. System Volume Information
Dim nCnt
On Error Resume Next
nCnt = oFolder.SubFolders|Files.Count
If 0 <> Err.Number Then
What shall we do with this Hidden/System folder?
...

or volatile resources (Network, Servers) compel extra efforts.

3. On Error Resume Next/ GoTo 0

Paul Randall has suggested reading the remarks in the script56.chm
file concerning the On Error statement. It brings back my long-ago
impression about how much of an afterthought error handling has been
for the BASIC developers. A single snippet of code, with no context,
serves as the only example of On Error Resume Next. No example of On
Error GoTo 0, let alone On Error GoTo <label of error handler>. Due

There is no "On Error Goto <Label>" in VBScript.

caution is mentioned that results are unpredictable depending on the
host environment if you don't have On Error statements in your code.

I found a better starting reference for this construct in the on the
MSDN site:
Unstructured Exception Handling Overview
http://msdn2.microsoft.com/en-us/library/sf1hwa21(VS.80).aspx

This deals with VB, not VBScript. So don't get overconfident/optimistic.

[...]

What I find particularly interesting is that none of these sources
make note of the possibility that if you have a runaway script, say,
you won't be able to break out of it with Ctrl-Break or Ctrl-C. If
you've run the script from an open command window, using cscript, Ctrl-
Break/Ctrl-C will work to break script execution. But if you've
started the script from the Windows shell, double clicking on the .vbs
file from Windows Explorer, you can't abort script execution except by
killing the task inside Task Manager. This is the problem that got me

That's one reason for avoiding wscript.exe.
There is a timeout feature (//T:<n>), but that can't be seen as a substitute
for decent signal handling.

[...]

4. Example code styles

I gave an example of a little wrapper I've started using in my code,
to protect against file I/O errors. Ekkehard gives many more examples
of coding style, one of which I had never seen before: a coherent
reason for using a multi-statement line. It used to be that you did
this in BASIC to save program memory. I always thought it made the
code uneditable. But he has Dimed the variable as he uses it, in the
below lines from his posting:

Dim sFSpec : sFSpec = "c:\exchange\data\20080201\maynotbethere.txt"
Dim oTS : Set oTS = oFS.OpenTextFile( sFSpec )
Dim bOk : bOk = False

I think that variables should be

declared (Dim) immediately before the first use (not in a block at the
beginning of the script/function)

initialized immediately

This isn't always feasible (nested loops, variables used for different
purposes) and makes it to easy to skip proper documentation (comments).
Perhaps

Dim sFSpec ''< list of computers to check; created by otherscript.vbs
sFSpec = "c:\exchange\data\20080201\maynotbethere.txt"

would be better

I'll have to experiment with that, as my style is to place all vars up
front, no matter where they're used, which makes for some awkward
jumping around.

That's why I don't like long Dim lists of variables at the start of the
script - some of them not even used anymore.

But he has the same economy of naming that I prefer:
'o' rather than 'obj' for object, 'b' rather than 'bln' for boolean,
's' rather than 'str' for string, and so forth - against VBS
recommended naming practice, but far preferable imho.


I see the hungarian prefix as a kind of "programmer, pay attention"
flag useful in not strictly typed languages. It helps me not to forget
the Set in object assignments or the type cast in boolean expressions.
But I wouldn't normally differentiate between int, long, single, or
double; just nNumber is enough for me. For member variables I use
"m_"

Public Property Let Name( sName )
...
m_sName = sName
End Property

but that may be caused by an overdosis of VC 6.0 programming.

The 'Do...Loop Until True' construct I also hadn't seen before. I use
While True ... Wend for blocks of code I want to preserve as a way of
"commenting them out" at runtime. I assumed he was doing that here as
well - except that if this defines a "block", then I assume it is the
"block of code" referred to in the remarks on "OERN" and so makes it
local. The 'Exit Do' confirms this usage, so it's another construct I

By "local" I just mean: not global at the start of the script, but as
a brace/bracket around the single statement that could fail.

The "Do..Loop Until True" is an attempt to add some kind of "On Error
Goto <Label>" to VBScript. All "Exit Do" will 'jump' to the statement
after the "Do" where the actual error handling can be centralized.
It's a hack, because you can't use nested "Do .. Loop"; if you perceive
that you spend more time on thinking about creative juggling of loop
constructs than on the problems your program should solve, it's time to
switch languages.

can use to help structure this error handling. It's a different way
of structuring the error handler, which in the examples I've seen (and
programmed myself) is placed sfter an Exit Sub statement, with error
handling code at the end. This is another compaction of code style,
where the error handling is embedded in with the code logic -
something I try mightily to avoid, generally by putting the handlers
at the end of the functions, and not using subs at all but rather

You can't do that in VBScript (without a hack; exiting out of a Sub
or Function could be an alternative to the "Exit <LoopType>" approach).

functions with return codes.

Overall, I really want to thank the three of you for your thoughtful
comments. This is great stuff. I'm going to mush along a bit longer
in VBS and incorporate your suggestions, but will be spending more
time in the future looking for a way to get the same work done (in
another language) that has error handling as a more intrinsic feature
of the language.

You are welcome; I thank you for posing such an interesting problem.
.



Relevant Pages

  • Re: Error Handling
    ... > Exit Function ... > What I'm confused on is error handling in vbs. ... > processing the script, which I don't want it to do. ...
    (microsoft.public.windows.server.scripting)
  • Error Handling
    ... Exit Function ... What I'm confused on is error handling in vbs. ... processing the script, which I don't want it to do. ...
    (microsoft.public.windows.server.scripting)
  • Re: Thoughts on current error handling best practice with VBScript
    ... And neither is "error handling" - my main ... Anthony Jones also recommends not using VBS if you're concerned about ... you've run the script from an open command window, using cscript, Ctrl- ... started the script from the Windows shell, ...
    (microsoft.public.scripting.vbscript)
  • RE: Error Handling
    ... > certain how error handling works in VBS. ... > Exit Function ... > What I'm confused on is error handling in vbs. ... > processing the script, which I don't want it to do. ...
    (microsoft.public.windows.server.scripting)
  • Re: Simple SQL Connection
    ... I'm a new to VBS and searched sites for vbs script but non of them ... run it from a command prompt. ... Dim cn ...
    (comp.lang.basic.visual.misc)