Error Trapping and Capturing Third-Party Output in WSH
(Page 1 of 4 )
In my last article we took an in-depth look at how to create log files for your scripts. In this article we are going to expand on that concept by teaching you how to trap events for a log file and how to capture third-party program output.
Chances are that if you’re logging the actions of one of your scripts, what you really want to know is what, if anything went wrong. After all, you’ve spent all that hard work making your life easier; it would be good to know it’s working as planned.
The easiest way to do this is to know what actions produced errors. The method of detecting errors with your script is known as error trapping. Error handling is how your script reacts to that error.
Understand that both error trapping and error handling are far beyond the scope of this article. I just want to introduce the ideas to you because it’s often useful to trap errors when executing third-party programs as you’ll see later in this article.
For our purposes we’re going to keep things simple. WSH provides us with the Err object. Any time an error occurs during execution, the error level is raised. We can make frequent checks to the Err object to know when something hasn’t gone as planned.
If we’re going to be handling our own errors, we don’t want our script to crash if one is encountered. Therefore, we need to tell the script debugger to let our script run no matter what. We do that by starting our script with the following line.
On Error Resume Next
This statement tells the script debugger to continue to the next operation whenever it encounters an operation that it can’t complete. While this line is typically found at the beginning of a script file, you can use it within individual functions and subroutines.
Properties
object.Number [= number]
object.Source [= string]
object.Description [= string]
object.HelpFile [= path]
object.HelpContext [= context]
Methods
object.Raise number[, source, description, helpfile, helpcontext]
object.Clear
The Err object has several methods and properties. The Number property is used to retrieve or set a runtime error number. The Source property returns the source of an error in the form of a class name or programmatic ID. The Description property is used to set or retrieve a text-based “friendly” description. The HelpFile and HelpContext properties are used to set and retrieve the location of local help file and a context ID within that help file, respectively.
Next: Trapping Errors >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer