When writing scripts in any language, it is critical to be able to catch and handle errors within your
script.
In Visual Basic script, there is a global error object that contains information about the last error
encountered.
Taking a look at the Wink Help file, looking under the Visual Basic scripting section.
Under Basic Principles of VB script, there is a keyword list and we can find the error object.
The error object is an intrinsic object with a global scope, so there's no need to create an instance.
It's important to note that in wink, all Visual Basic scripts in a system are compiled together in
one big script when you go to runtime.
Therefore, an error that is not handled in one script could affect another script without you realizing
it, and this can be very difficult to troubleshoot.
So the error object gives you a number which indicates the type of error that happens.
And it also allows you to get the source and description of the error.
And you can raise your own errors as well as clear the current error.
So let's take a quick look at an example of using the error object.
Here I have a button called Division test and it merely does division and prints out the results.
There's a hard coded number called my number.
It's hard coded to six.
There is a variable called test division and then a variable called divisor, which gets this value
from an internal tag that we are going to enter on the screen.
It then performs the division dividing my number by divisor and storing the results in test division.
And then it uses my runtime trace to just print out the results.
The test for errors button just checks the global Error objects number to see if it's anything other
than zero, indicating an error has occurred, and if it does, it prints out information about the
error.
If not, it just prints out there are no errors.
Again, this is a completely different subroutine than the one on this button.
So let's go to runtime and see what we've got.
First, I'll just test for errors and there are no current errors.
And then I'm going to put something valid in divisor and press our division test.
And it just prints out six divided by two is equal to three.
No errors there.
But if I introduce an error a divide by zero error then I'll hit the button.
Notice that when CC prints out information about the error and this is helpful, it tells you what page
the error occurred on, what object and event that the error occurred on, and what the error was.
But notice that my print statement did not execute.
But what's interesting is if I hit test for errors, the error object has not been cleared in this case.
So if we have another script that is checking for errors, then it mistakenly may have an error that
was caused by a completely different script.
So what we want to do is handle our own errors using the error object.
So when we're handling our own errors, there are three things that we need to do in this script.
The first thing is we need to disable the when CC error reporting so that we can handle it.
The second thing we need to do is check for errors at different areas of the script where they may occur.
And the third thing is to handle the error and then clear the error object.
So in order to do the first thing before we get into any real code, we are going to declare on error
resume next.
This prevents when CC from handling the errors.
Now if I just leave it like this.
Let's watch what happens so that you can understand it better.
I'll just click okay.
I'll hit save and run.
First.
We will give it a valid result that looks good and then an error result.
All right.
It still prints out.
But notice we don't get anything.
So we haven't caught the error.
All we've done is turned off Winky's ability to handle it.
So now let's go handle that error.
Go back into the script.
And right after we do, the division is a good place to test for errors, and what we can do is make
use of the error number property.
So I'll just paste in the code here.
It just tests error number to be anything other than zero.
Now you may see some code online where they do this, but that's not going to catch all errors.
So I always look for the not equal.
And then the other thing that we can do I've got error number and error description.
But there is also an error source that we can use.
So I'm just going to paste that in here.
And this isn't specifically spelled out in the help file.
But I found out that it does support it.
And so now if there is an error then we're simply just going to print out that it exists.
We're not going to do anything to go rerun the script.
And then importantly, we're going to clear the error so it doesn't affect any other scripts.
And then finally, if there was an error, we are just going to exit sub, which gets us out of the
script because this is going to be meaningless.
So now I'll just click okay.
We'll hit save and go to runtime.
And first we will do the good test.
Everything's okay.
And then we will introduce an error.
And it prints out it was error number 11.
The source is Microsoft VBScript runtime error and the actual error was division by zero.
If we print out test for errors, you can see that there are no current errors because we did clear
the error object.
And if we run this again then we have a good result.
Now let's go back to our Edo script and see how we can implement checking for errors and handling them
using the error object.
So before we get into any real code we put in on error resume.
Next.
And this disables the automatic error reporting so we can handle it.
Once you do this you do need to make sure you have code in to handle the scripts because since when
CC isn't handling it, it's not going to print out any information.
So our first point where we might have an error is when we try to open our connection.
So we just test if error number is zero.
And if it is we'll continue on with the script.
And the second place we may encounter an error is when we execute our SQL command with our record set.
So we open our record set and then again check if we have no errors.
Then we will continue on and print out the results.
After that loop, we test and see if there is anything other than zero in there and just print out the
information.
And so these two lines are where the end if statements are for our test for the connection object and
the record set object.
So if there is a connection error it just skips to this line.
If there's a record set error then it just skips to this line.
And then we check for errors, print out the information and clear.
And then the only code is going to be our housekeeping code, where we clean up our record setting connection,
which we want to do anyway.
So now let's go introduce some errors and see how it does.
So the first error I'm going to put in a non-existent table name and see what kind of error we get.
I'll click okay.
Save and run.
And when we run the script, notice that the error is not greater than zero.
It's a really big less than zero number.
So that's why you always check for does not equal zero rather than greater than zero.
And then it tells you that the Ole DB provider is throwing the error and it's an invalid object name.
Next let's go back and test if our connection string is wrong.
So I will fix this error and then I will just put Z-z-z here.
So now we have an invalid provider and we'll click okay.
Save and go to runtime.
Test our script.
Now it tells us that the problem is that Adodb connection and it says the provider cannot be found.
And so we'll go back to the script and of course we can fix this.
Now, of course, if there is an error down here.
So let's just say that we're putting something incorrect here.
I'll just put some nonsense in there.
Click okay.
Save it and run it.
Okay, so now it will print out the headers.
It can't print out the loops because it doesn't have the correct code.
And then it tells you there's a VBScript runtime error.
It just says object doesn't support this property or method.
So that is kind of a generic error.
But you should be able to go back and find that.
So once we fix this now let's see if it runs without errors we'll save.
And run.
And there's our code.
So now we have an adult script that can detect and handle errors.
In the next assignment, you're going to get a chance to add error handling to our Ado script.
So I'll see you there.