Re: Thoughts on current error handling best practice with VBScript
- From: "ekkehard.horner" <ekkehard.horner@xxxxxxxx>
- Date: Sat, 02 Feb 2008 20:33:15 +0100
FUBARinSFO schrieb:
Hi:[...]
While confused that I wasn't getting runtiime error messages when I
tried to write to files with invalid paths, I poked around see that
error handling is generally absent in vbscript. Since I make so few
errors in my programs, this has never been an issue for me....not.
Is there a document somewhere describing best practice with regard to
error handling in vbscript? With regard to defensive programming
practices that should be observed.
Finally, in self protection against writing to files/folders that
don't exist, it looks like I'll be wrapping the write statements in
fso.FolderExists(sPathspec) tests. Is that about right?
The VBScript way of errorhandling is to abort the script. You can disable this
feature by using "On Error Resume Next" globally (at the top of your code).
That is like driving with closed eyes to avoid being dazzled by the headlights
of other cars.
You should make sure that your script runs correctly given normal conditions.
E.g.: If a program reads a config file in its own directory, it's ok just
to open it
Dim oTS : Set oTS = oFS.OpenTextFile( ".\config.txt" )
a user who deletes that config file deserves what he gets. If the program
reads a local data file that may not be there, test before you jump
Dim sFSpec : sFSpec = "c:\exchange\data\20080201\maynotbethere.txt"
If oFS.FileExists( sFSpec ) Then
Dim oTS : Set oTS = oFS.OpenTextFile( sFSpec )
...
Else
MsgBox sFSpec & " not found! ..."
...
End If
If the user succeeds in deleting the file after your test but before
your script has processed it - let him blame himself.
If you have to make arrangements against bad luck - your script writes
to a file on a remote server - use OERN in a strictly local way:
Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sFSpec : sFSpec = "\\fangorn\tmp\test\something.txt"
Dim bOk : bOk = False
Dim oTS, nCnt
If oFS.FolderExists( oFS.GetParentFolderName( sFSpec ) ) Then
Do
On Error Resume Next
Set oTS = oFS.CreateTextFile( sFSpec, True )
If 0 <> Err.Number Then Exit Do
On Error GoTo 0
For nCnt = 1 To 10
' 20 lines of building output ....
On Error Resume Next
oTS.WriteLine nCnt
If 0 <> Err.Number Then Exit Do
On Error GoTo 0
' 40 lines of something else ....
Next
On Error Resume Next
oTS.Close
If 0 <> Err.Number Then Exit Do
On Error GoTo 0
bOk = True
MsgBox "Looks good!"
Loop Until True
If Not bOk Then
MsgBox Err.Number & ": " & Err.Description
End If
Else
MsgBox oFS.GetParentFolderName( sFSpec ) & " not found! ..."
End If
(BTW: If you keep your OERN/OEG0 close together (just one line of code
that could fail) you can skip all the philological research and testing
Paul proposed.)
If you now think that this is a bit too much of hassle, I agree. So for
tasks that need more than a few checks by normal code and perhaps one
or two local OERNs, you should follow Anthony's advice: switch to another
language.
.
- Follow-Ups:
- Re: Thoughts on current error handling best practice with VBScript
- From: FUBARinSFO
- Re: Thoughts on current error handling best practice with VBScript
- References:
- Thoughts on current error handling best practice with VBScript
- From: FUBARinSFO
- Thoughts on current error handling best practice with VBScript
- Prev by Date: Re: RDP and Login to remote PC
- Next by Date: Move Script based on Modified date
- Previous by thread: Re: Thoughts on current error handling best practice with VBScript
- Next by thread: Re: Thoughts on current error handling best practice with VBScript
- Index(es):
Relevant Pages
|
Loading