1
00:00:00,840 --> 00:00:05,540
In the last lecture, I said we'd make a routine that handles errors, so let's do that.

2
00:00:05,910 --> 00:00:12,330
What I'm going to do is come down here to my main function and create a new path and a new euro that

3
00:00:12,330 --> 00:00:12,840
I'm going to handle.

4
00:00:12,840 --> 00:00:14,100
And I'll call this divide.

5
00:00:15,240 --> 00:00:16,000
And up here.

6
00:00:16,620 --> 00:00:20,970
Of course, we need to point that to the correct function divide, which doesn't exist yet, but that's

7
00:00:20,970 --> 00:00:21,500
no problem.

8
00:00:21,510 --> 00:00:24,390
We can create one func divide.

9
00:00:25,110 --> 00:00:27,510
And because this is a handler, it has two parameters.

10
00:00:27,510 --> 00:00:36,630
The response writer W is KTP Dot response writer and a pointer to a request.

11
00:00:37,770 --> 00:00:39,000
And that's supposed to be a pointer.

12
00:00:39,000 --> 00:00:42,950
So let's make it a pointer and give her curly braces.

13
00:00:43,650 --> 00:00:46,890
And now here I want to call a function that does division.

14
00:00:46,890 --> 00:00:51,320
And the reason I'm doing division is that dividing by zero is generally a bad idea.

15
00:00:51,690 --> 00:00:53,070
So let's create another function.

16
00:00:53,190 --> 00:01:01,590
Func divide values that takes two parameters, X and Y, but this time I'm going to make them float's.

17
00:01:02,310 --> 00:01:06,180
I'll make it a float thirty two, which is another of the basic data types.

18
00:01:06,180 --> 00:01:09,840
So far we have seen strings, we have seen INTs and float.

19
00:01:09,840 --> 00:01:11,550
Thirty two is just a floating point number.

20
00:01:12,010 --> 00:01:16,890
I could have used float 64 if I wanted, but it doesn't matter because I'm going to just use this for

21
00:01:16,890 --> 00:01:17,640
a quick test.

22
00:01:18,870 --> 00:01:21,630
That function is going to return to things.

23
00:01:21,970 --> 00:01:26,760
It's going to return a float thirty two and it's going to return an error.

24
00:01:26,760 --> 00:01:28,870
And if there is no error, it should return nil.

25
00:01:29,280 --> 00:01:30,600
So how do we do this?

26
00:01:30,810 --> 00:01:37,200
First of all, well, the basic logic is going to be the result is assigned X divided by what?

27
00:01:38,160 --> 00:01:44,640
And I'll return Y and nil and that actually works just fine.

28
00:01:44,970 --> 00:01:45,780
I not return.

29
00:01:45,780 --> 00:01:48,380
Why return result and nil.

30
00:01:48,390 --> 00:01:49,380
And that works just fine.

31
00:01:49,620 --> 00:01:52,100
But there's no actual error generated here.

32
00:01:52,110 --> 00:01:57,740
So what I need to check is to make sure that Y is greater than or equal to zero.

33
00:01:57,840 --> 00:02:04,770
So all I have to do is say if Y is less than or equal to zero then I want to stop, I want to generate

34
00:02:04,770 --> 00:02:05,280
an error.

35
00:02:05,280 --> 00:02:11,040
And I can do that by saying error, which is a new variable, is assigned the value of and this is a

36
00:02:11,040 --> 00:02:18,870
built in function errors new and some strength cannot divide by zero.

37
00:02:19,230 --> 00:02:29,880
And at that point I want to return and I'll just say float thirty two I point zero and er so that's

38
00:02:29,880 --> 00:02:30,150
that.

39
00:02:30,510 --> 00:02:34,680
If this, if this function gets the wrong thing here, errors don't new.

40
00:02:35,380 --> 00:02:36,300
Let's try that again.

41
00:02:36,810 --> 00:02:40,130
Errors not new.

42
00:02:40,140 --> 00:02:40,640
There we are.

43
00:02:41,670 --> 00:02:43,190
It just had to import it up here.

44
00:02:43,200 --> 00:02:47,850
Errors return return to zero and the error.

45
00:02:47,850 --> 00:02:48,960
I don't care about the zero.

46
00:02:48,960 --> 00:02:51,780
It's the error that I'll be checking for up here and divide.

47
00:02:51,900 --> 00:02:53,160
I'm not doing anything yet.

48
00:02:53,160 --> 00:02:56,850
So let's actually call this function and I'm going to get two values from it.

49
00:02:56,850 --> 00:03:02,880
I'll get my float, which I'll call F and I'll get an error that will be nil if there is no error but

50
00:03:02,880 --> 00:03:05,040
will be not nil if it is an error.

51
00:03:05,520 --> 00:03:11,010
And that is I get those two values by calling the function I just created divide values and I'll put

52
00:03:11,010 --> 00:03:16,200
in one hundred and one hundred point zero because it's the float and ten point zero.

53
00:03:17,340 --> 00:03:20,490
If error is not equal to nil, then what do I do.

54
00:03:20,490 --> 00:03:23,190
I want to write to the screen so I can just use my standard.

55
00:03:24,800 --> 00:03:28,250
Format or format F, print F.

56
00:03:30,580 --> 00:03:38,590
You cannot divide by zero, and at that point, you think I could just stop because, you know, well,

57
00:03:38,590 --> 00:03:42,160
we've got the air, we've written the scream, let's leave it like that and show you why you can't do

58
00:03:42,160 --> 00:03:46,650
that if there is no air that I actually want to write the result to the screen.

59
00:03:46,690 --> 00:03:55,000
And I can do that by format F F, format s, print F.

60
00:03:56,980 --> 00:04:00,400
And what I'm going to put in is a string and it will be a float.

61
00:04:00,520 --> 00:04:10,930
So the placeholder for float is percent F divided by percent F is percent F and then I have to of course

62
00:04:10,930 --> 00:04:15,250
fill in my placeholders and those would be X, Y and F.

63
00:04:17,020 --> 00:04:17,480
All right.

64
00:04:17,770 --> 00:04:19,420
So what is wrong with this?

65
00:04:19,480 --> 00:04:21,480
We have got to put my response right here in front of it.

66
00:04:25,400 --> 00:04:27,770
Not enough for the wrong place for my place.

67
00:04:27,850 --> 00:04:28,640
Let's try that again.

68
00:04:28,940 --> 00:04:30,560
I think I need more caffeine once again.

69
00:04:32,230 --> 00:04:32,890
That's what I want.

70
00:04:33,200 --> 00:04:39,740
Now, if I run this and I'm going to right now, everything compiles undefined X and undefined Y, let's

71
00:04:39,740 --> 00:04:40,730
find X and Y.

72
00:04:43,010 --> 00:04:43,490
Oh, yes.

73
00:04:43,490 --> 00:04:46,940
I didn't actually have values for X and Y, so I'll just hard to lose.

74
00:04:46,940 --> 00:04:49,580
One hundred and ten.

75
00:04:51,630 --> 00:04:52,730
I'll try that again.

76
00:04:54,800 --> 00:04:55,990
Everything compiles.

77
00:04:56,000 --> 00:05:00,290
I go back to my web browser, I reload the page for the home page that works great.

78
00:05:00,620 --> 00:05:08,930
I go to the about page that does what I want and then I go to the divide page and one hundred divided

79
00:05:08,930 --> 00:05:11,190
by ten is ten and that's correct.

80
00:05:11,240 --> 00:05:14,660
OK, let's go back here and let's try to divide by zero.

81
00:05:15,260 --> 00:05:22,520
So I'll put in zero point zero here and up here as well, or I'm actually doing the math and that should

82
00:05:22,520 --> 00:05:23,330
throw an error.

83
00:05:23,360 --> 00:05:23,780
Right.

84
00:05:24,030 --> 00:05:27,410
Let's see if it does restart the application.

85
00:05:27,410 --> 00:05:29,060
Go back here and reload the page.

86
00:05:29,060 --> 00:05:31,820
And here's why we can't do what I did just a minute ago.

87
00:05:32,390 --> 00:05:34,280
Cannot divide by zero immediately.

88
00:05:34,280 --> 00:05:36,230
Followed by one hundred.

89
00:05:36,230 --> 00:05:37,780
Divided by zero is zero.

90
00:05:37,790 --> 00:05:39,620
Well, that's not a true answer.

91
00:05:40,610 --> 00:05:42,260
Let's find out why I did that here.

92
00:05:42,260 --> 00:05:49,100
I have on line thirty in my application format F Pranav then the response writer then cannot divide

93
00:05:49,100 --> 00:05:50,810
by zero at that point.

94
00:05:50,810 --> 00:05:51,860
I want to return.

95
00:05:51,890 --> 00:05:53,960
I want to stop executing this function.

96
00:05:54,410 --> 00:05:55,310
So let's try it again.

97
00:05:59,660 --> 00:06:03,290
Reload cannot divide by zero, and that's what I'm looking for.

98
00:06:03,410 --> 00:06:08,150
All right, that's why we checked for errors already in the next election, we'll pick up with something

99
00:06:08,150 --> 00:06:09,530
a little more sophisticated.
