1
00:00:05,750 --> 00:00:11,270
When writing scripts in any language, it is critical to be able to catch and handle errors within your

2
00:00:11,270 --> 00:00:12,140
script.

3
00:00:12,140 --> 00:00:18,440
In Visual Basic script, there is a global error object that contains information about the last error

4
00:00:18,440 --> 00:00:19,220
encountered.

5
00:00:19,250 --> 00:00:26,240
Taking a look at the Wink Help file, looking under the Visual Basic scripting section.

6
00:00:26,240 --> 00:00:32,720
Under Basic Principles of VB script, there is a keyword list and we can find the error object.

7
00:00:32,720 --> 00:00:38,210
The error object is an intrinsic object with a global scope, so there's no need to create an instance.

8
00:00:38,240 --> 00:00:45,080
It's important to note that in wink, all Visual Basic scripts in a system are compiled together in

9
00:00:45,080 --> 00:00:47,360
one big script when you go to runtime.

10
00:00:47,360 --> 00:00:52,730
Therefore, an error that is not handled in one script could affect another script without you realizing

11
00:00:52,730 --> 00:00:55,460
it, and this can be very difficult to troubleshoot.

12
00:00:55,460 --> 00:01:01,040
So the error object gives you a number which indicates the type of error that happens.

13
00:01:01,040 --> 00:01:05,810
And it also allows you to get the source and description of the error.

14
00:01:05,810 --> 00:01:09,590
And you can raise your own errors as well as clear the current error.

15
00:01:09,590 --> 00:01:13,910
So let's take a quick look at an example of using the error object.

16
00:01:13,910 --> 00:01:21,320
Here I have a button called Division test and it merely does division and prints out the results.

17
00:01:21,320 --> 00:01:23,540
There's a hard coded number called my number.

18
00:01:23,540 --> 00:01:25,310
It's hard coded to six.

19
00:01:25,310 --> 00:01:32,000
There is a variable called test division and then a variable called divisor, which gets this value

20
00:01:32,000 --> 00:01:35,840
from an internal tag that we are going to enter on the screen.

21
00:01:35,840 --> 00:01:43,490
It then performs the division dividing my number by divisor and storing the results in test division.

22
00:01:43,490 --> 00:01:48,020
And then it uses my runtime trace to just print out the results.

23
00:01:48,020 --> 00:01:54,140
The test for errors button just checks the global Error objects number to see if it's anything other

24
00:01:54,140 --> 00:02:00,500
than zero, indicating an error has occurred, and if it does, it prints out information about the

25
00:02:00,500 --> 00:02:00,950
error.

26
00:02:00,950 --> 00:02:03,650
If not, it just prints out there are no errors.

27
00:02:03,650 --> 00:02:08,660
Again, this is a completely different subroutine than the one on this button.

28
00:02:08,990 --> 00:02:11,960
So let's go to runtime and see what we've got.

29
00:02:12,900 --> 00:02:16,830
First, I'll just test for errors and there are no current errors.

30
00:02:17,280 --> 00:02:23,490
And then I'm going to put something valid in divisor and press our division test.

31
00:02:23,610 --> 00:02:27,690
And it just prints out six divided by two is equal to three.

32
00:02:27,720 --> 00:02:29,370
No errors there.

33
00:02:29,400 --> 00:02:35,130
But if I introduce an error a divide by zero error then I'll hit the button.

34
00:02:35,130 --> 00:02:41,280
Notice that when CC prints out information about the error and this is helpful, it tells you what page

35
00:02:41,280 --> 00:02:47,400
the error occurred on, what object and event that the error occurred on, and what the error was.

36
00:02:47,400 --> 00:02:51,390
But notice that my print statement did not execute.

37
00:02:51,390 --> 00:02:58,470
But what's interesting is if I hit test for errors, the error object has not been cleared in this case.

38
00:02:58,470 --> 00:03:05,460
So if we have another script that is checking for errors, then it mistakenly may have an error that

39
00:03:05,460 --> 00:03:07,800
was caused by a completely different script.

40
00:03:07,800 --> 00:03:13,500
So what we want to do is handle our own errors using the error object.

41
00:03:13,500 --> 00:03:18,930
So when we're handling our own errors, there are three things that we need to do in this script.

42
00:03:18,930 --> 00:03:24,990
The first thing is we need to disable the when CC error reporting so that we can handle it.

43
00:03:24,990 --> 00:03:31,950
The second thing we need to do is check for errors at different areas of the script where they may occur.

44
00:03:31,950 --> 00:03:36,450
And the third thing is to handle the error and then clear the error object.

45
00:03:36,450 --> 00:03:44,550
So in order to do the first thing before we get into any real code, we are going to declare on error

46
00:03:44,550 --> 00:03:45,450
resume next.

47
00:03:45,450 --> 00:03:48,990
This prevents when CC from handling the errors.

48
00:03:48,990 --> 00:03:51,210
Now if I just leave it like this.

49
00:03:51,210 --> 00:03:54,450
Let's watch what happens so that you can understand it better.

50
00:03:54,450 --> 00:03:55,890
I'll just click okay.

51
00:03:55,890 --> 00:03:57,840
I'll hit save and run.

52
00:03:57,840 --> 00:03:58,410
First.

53
00:03:58,410 --> 00:04:04,260
We will give it a valid result that looks good and then an error result.

54
00:04:04,260 --> 00:04:04,650
All right.

55
00:04:04,650 --> 00:04:05,700
It still prints out.

56
00:04:05,700 --> 00:04:07,590
But notice we don't get anything.

57
00:04:07,590 --> 00:04:09,630
So we haven't caught the error.

58
00:04:09,630 --> 00:04:13,530
All we've done is turned off Winky's ability to handle it.

59
00:04:13,530 --> 00:04:15,930
So now let's go handle that error.

60
00:04:16,170 --> 00:04:17,880
Go back into the script.

61
00:04:18,460 --> 00:04:25,720
And right after we do, the division is a good place to test for errors, and what we can do is make

62
00:04:25,720 --> 00:04:28,510
use of the error number property.

63
00:04:28,510 --> 00:04:31,060
So I'll just paste in the code here.

64
00:04:31,060 --> 00:04:34,900
It just tests error number to be anything other than zero.

65
00:04:34,900 --> 00:04:40,540
Now you may see some code online where they do this, but that's not going to catch all errors.

66
00:04:40,540 --> 00:04:43,780
So I always look for the not equal.

67
00:04:43,780 --> 00:04:48,580
And then the other thing that we can do I've got error number and error description.

68
00:04:48,580 --> 00:04:52,270
But there is also an error source that we can use.

69
00:04:52,600 --> 00:04:54,970
So I'm just going to paste that in here.

70
00:04:55,240 --> 00:05:00,880
And this isn't specifically spelled out in the help file.

71
00:05:00,880 --> 00:05:02,950
But I found out that it does support it.

72
00:05:02,950 --> 00:05:08,470
And so now if there is an error then we're simply just going to print out that it exists.

73
00:05:08,470 --> 00:05:11,050
We're not going to do anything to go rerun the script.

74
00:05:11,050 --> 00:05:16,060
And then importantly, we're going to clear the error so it doesn't affect any other scripts.

75
00:05:16,060 --> 00:05:23,290
And then finally, if there was an error, we are just going to exit sub, which gets us out of the

76
00:05:23,290 --> 00:05:26,530
script because this is going to be meaningless.

77
00:05:26,530 --> 00:05:29,020
So now I'll just click okay.

78
00:05:29,650 --> 00:05:32,800
We'll hit save and go to runtime.

79
00:05:32,800 --> 00:05:36,220
And first we will do the good test.

80
00:05:36,220 --> 00:05:37,450
Everything's okay.

81
00:05:37,450 --> 00:05:39,940
And then we will introduce an error.

82
00:05:40,760 --> 00:05:44,210
And it prints out it was error number 11.

83
00:05:44,240 --> 00:05:50,300
The source is Microsoft VBScript runtime error and the actual error was division by zero.

84
00:05:50,330 --> 00:05:56,780
If we print out test for errors, you can see that there are no current errors because we did clear

85
00:05:56,780 --> 00:05:58,190
the error object.

86
00:05:58,190 --> 00:06:02,330
And if we run this again then we have a good result.

87
00:06:02,980 --> 00:06:10,030
Now let's go back to our Edo script and see how we can implement checking for errors and handling them

88
00:06:10,030 --> 00:06:11,920
using the error object.

89
00:06:12,010 --> 00:06:17,500
So before we get into any real code we put in on error resume.

90
00:06:17,500 --> 00:06:18,130
Next.

91
00:06:18,130 --> 00:06:22,000
And this disables the automatic error reporting so we can handle it.

92
00:06:22,000 --> 00:06:27,250
Once you do this you do need to make sure you have code in to handle the scripts because since when

93
00:06:27,250 --> 00:06:31,480
CC isn't handling it, it's not going to print out any information.

94
00:06:31,570 --> 00:06:37,150
So our first point where we might have an error is when we try to open our connection.

95
00:06:37,330 --> 00:06:40,540
So we just test if error number is zero.

96
00:06:40,540 --> 00:06:43,600
And if it is we'll continue on with the script.

97
00:06:43,600 --> 00:06:49,780
And the second place we may encounter an error is when we execute our SQL command with our record set.

98
00:06:49,780 --> 00:06:54,490
So we open our record set and then again check if we have no errors.

99
00:06:54,490 --> 00:06:58,360
Then we will continue on and print out the results.

100
00:06:59,060 --> 00:07:06,440
After that loop, we test and see if there is anything other than zero in there and just print out the

101
00:07:06,440 --> 00:07:07,370
information.

102
00:07:07,370 --> 00:07:15,500
And so these two lines are where the end if statements are for our test for the connection object and

103
00:07:15,500 --> 00:07:16,610
the record set object.

104
00:07:16,610 --> 00:07:20,210
So if there is a connection error it just skips to this line.

105
00:07:20,210 --> 00:07:24,920
If there's a record set error then it just skips to this line.

106
00:07:24,920 --> 00:07:29,450
And then we check for errors, print out the information and clear.

107
00:07:29,450 --> 00:07:35,450
And then the only code is going to be our housekeeping code, where we clean up our record setting connection,

108
00:07:35,450 --> 00:07:37,520
which we want to do anyway.

109
00:07:37,850 --> 00:07:42,500
So now let's go introduce some errors and see how it does.

110
00:07:42,500 --> 00:07:50,870
So the first error I'm going to put in a non-existent table name and see what kind of error we get.

111
00:07:51,670 --> 00:07:52,840
I'll click okay.

112
00:07:53,870 --> 00:07:56,090
Save and run.

113
00:07:56,090 --> 00:08:02,000
And when we run the script, notice that the error is not greater than zero.

114
00:08:02,030 --> 00:08:04,460
It's a really big less than zero number.

115
00:08:04,460 --> 00:08:09,920
So that's why you always check for does not equal zero rather than greater than zero.

116
00:08:09,920 --> 00:08:17,300
And then it tells you that the Ole DB provider is throwing the error and it's an invalid object name.

117
00:08:17,300 --> 00:08:22,160
Next let's go back and test if our connection string is wrong.

118
00:08:22,870 --> 00:08:29,140
So I will fix this error and then I will just put Z-z-z here.

119
00:08:29,140 --> 00:08:33,790
So now we have an invalid provider and we'll click okay.

120
00:08:34,890 --> 00:08:36,870
Save and go to runtime.

121
00:08:37,610 --> 00:08:39,260
Test our script.

122
00:08:39,870 --> 00:08:46,830
Now it tells us that the problem is that Adodb connection and it says the provider cannot be found.

123
00:08:46,830 --> 00:08:51,420
And so we'll go back to the script and of course we can fix this.

124
00:08:52,020 --> 00:08:55,380
Now, of course, if there is an error down here.

125
00:08:55,710 --> 00:09:01,020
So let's just say that we're putting something incorrect here.

126
00:09:01,790 --> 00:09:03,620
I'll just put some nonsense in there.

127
00:09:03,620 --> 00:09:04,670
Click okay.

128
00:09:05,840 --> 00:09:07,970
Save it and run it.

129
00:09:08,030 --> 00:09:11,060
Okay, so now it will print out the headers.

130
00:09:11,060 --> 00:09:14,840
It can't print out the loops because it doesn't have the correct code.

131
00:09:14,840 --> 00:09:18,710
And then it tells you there's a VBScript runtime error.

132
00:09:18,710 --> 00:09:21,440
It just says object doesn't support this property or method.

133
00:09:21,440 --> 00:09:23,990
So that is kind of a generic error.

134
00:09:23,990 --> 00:09:27,050
But you should be able to go back and find that.

135
00:09:27,350 --> 00:09:33,890
So once we fix this now let's see if it runs without errors we'll save.

136
00:09:34,540 --> 00:09:35,320
And run.

137
00:09:35,320 --> 00:09:36,640
And there's our code.

138
00:09:36,640 --> 00:09:42,250
So now we have an adult script that can detect and handle errors.

139
00:09:44,130 --> 00:09:50,310
In the next assignment, you're going to get a chance to add error handling to our Ado script.

140
00:09:50,310 --> 00:09:51,930
So I'll see you there.
