1
00:00:00,750 --> 00:00:05,250
You guys in this video I want to discuss error handling and Javascript and for the most part what a

2
00:00:05,250 --> 00:00:06,920
try catch block is.

3
00:00:07,110 --> 00:00:14,130
So a try block lets us do something let's just write some bit of code and test for errors and the catch

4
00:00:14,130 --> 00:00:17,180
block lets us handle those errors if there are any.

5
00:00:17,220 --> 00:00:19,710
OK we can also throw our own errors.

6
00:00:19,710 --> 00:00:24,280
So let's go ahead and dive into this and let's go ahead and say try.

7
00:00:25,110 --> 00:00:27,270
And then after that we'll say catch

8
00:00:30,140 --> 00:00:32,870
and then this is going to take an error object.

9
00:00:32,870 --> 00:00:33,460
OK.

10
00:00:33,500 --> 00:00:37,590
Sometimes you just see sometimes just c e r r error.

11
00:00:37,770 --> 00:00:40,020
I like to use e in this case.

12
00:00:40,460 --> 00:00:43,900
And then let's just put something in here that will fail.

13
00:00:44,090 --> 00:00:44,420
All right.

14
00:00:44,420 --> 00:00:48,970
So let's say my function.

15
00:00:49,000 --> 00:00:56,620
Right so we're calling My function Now why will this fail because there is no declared to my function.

16
00:00:56,930 --> 00:00:58,760
It's actually going to be a reference error.

17
00:00:58,800 --> 00:01:03,920
OK there's different types of errors in javascript this will produce a

18
00:01:06,710 --> 00:01:14,900
reference error such search capital so reference are all right and then down here we can decide what

19
00:01:14,900 --> 00:01:17,270
we want to do when this fails.

20
00:01:17,270 --> 00:01:21,430
So what I'm going to do is just console dot log.

21
00:01:21,680 --> 00:01:23,460
And when a console log.

22
00:01:23,540 --> 00:01:25,780
E see what that gives us.

23
00:01:25,790 --> 00:01:27,310
All right so very simple code.

24
00:01:27,320 --> 00:01:33,470
Let's go ahead and save this and run it and we get a reference error down here that says My function

25
00:01:33,470 --> 00:01:35,150
is not defined.

26
00:01:35,240 --> 00:01:35,620
All right.

27
00:01:35,650 --> 00:01:44,390
Now what's really nice about try catch is that we can handle errors without stopping the entire script.

28
00:01:44,390 --> 00:01:57,110
So down here I'm going to say console log and I'm just going to say program continues OK and save and

29
00:01:57,110 --> 00:01:59,360
you can see that you know we get the error.

30
00:01:59,480 --> 00:02:00,170
We catch it.

31
00:02:00,170 --> 00:02:03,870
We do we do something with it which in our case is just console logging.

32
00:02:03,890 --> 00:02:05,260
And then we continue.

33
00:02:05,300 --> 00:02:12,920
Now if I were to take this my function and just go like that and save we just get this nasty error down

34
00:02:12,920 --> 00:02:17,850
here and read and the script doesn't continue we don't get our console log.

35
00:02:17,870 --> 00:02:23,000
All right so try catches a very elegant way of handling errors.

36
00:02:23,000 --> 00:02:30,650
Now if you want to just get the message my function is not defined and I get this to the name reference

37
00:02:30,650 --> 00:02:31,150
error.

38
00:02:31,400 --> 00:02:35,620
You could do just go ahead and copy this down.

39
00:02:35,630 --> 00:02:43,430
You could do the dot message and comment this so and save.

40
00:02:43,430 --> 00:02:46,160
And now you see we just get my function is not defined.

41
00:02:46,160 --> 00:02:52,070
So if you didn't want to just log this you could if for some reason if you wanted to output it to the

42
00:02:52,070 --> 00:02:54,080
browser into the dawn you could do that.

43
00:02:54,080 --> 00:02:56,170
You can do whatever you want with it.

44
00:02:56,210 --> 00:02:56,590
All right.

45
00:02:56,590 --> 00:03:03,020
Now if you wanted to just yet type or actually it's called the name which is the type of error you could

46
00:03:03,020 --> 00:03:07,940
do it name it and save and you see that it'll give you reference error.

47
00:03:08,330 --> 00:03:12,190
OK you can also test to see what kind of error is.

48
00:03:12,200 --> 00:03:23,960
So if we were to say console log and inside here will say e and we can use instance of code can use

49
00:03:23,960 --> 00:03:29,700
the instance of operator and say no reference or

50
00:03:33,340 --> 00:03:35,990
kind will save and you can see we get true.

51
00:03:36,190 --> 00:03:42,760
Now if we were to test and see if it was a type error which is a different kind of error we get false.

52
00:03:42,760 --> 00:03:48,370
All right so you can test to see that but I'm just going to go ahead and comment those out and leave

53
00:03:48,460 --> 00:03:49,170
just the

54
00:03:52,730 --> 00:03:58,850
now there is a third piece you could put onto this block and that's finally and all this does.

55
00:03:58,850 --> 00:04:01,180
Very simple it just runs no matter what.

56
00:04:01,190 --> 00:04:04,910
No matter what the result of anything up here is this will still run.

57
00:04:04,910 --> 00:04:19,010
So we'll say console log and we'll just say finally runs regardless of result and save and you can see

58
00:04:19,010 --> 00:04:20,120
that that's going to run.

59
00:04:20,300 --> 00:04:23,730
No matter what happens up here.

60
00:04:23,760 --> 00:04:27,200
Now if we wanted to produce a different type of error.

61
00:04:28,410 --> 00:04:35,250
So it's produce a type error.

62
00:04:35,250 --> 00:04:42,740
So what it what we could do here is take now and we could try to call something from it like my function.

63
00:04:42,750 --> 00:04:44,950
All right we can't call something from now.

64
00:04:44,970 --> 00:04:46,490
This is going to cause a type error.

65
00:04:46,500 --> 00:04:52,890
So let's save it and run it and you can see this cannot read our type error cannot read properly my

66
00:04:52,890 --> 00:04:54,650
function of no.

67
00:04:54,960 --> 00:04:55,580
OK.

68
00:04:56,790 --> 00:04:59,380
And you can make this you know this part.

69
00:04:59,390 --> 00:05:00,870
This right here whatever you want.

70
00:05:00,870 --> 00:05:02,460
You don't even have to use IE.

71
00:05:02,460 --> 00:05:03,360
You could say

72
00:05:06,070 --> 00:05:09,450
console log and you could say

73
00:05:12,920 --> 00:05:17,640
it's Nelse stupid and that's what that's what it'll show you down here.

74
00:05:17,650 --> 00:05:23,800
All right so you can format these errors however you want and if you wanted to go back there you could

75
00:05:23,800 --> 00:05:36,700
actually put in some tactics here and you could put a dot name like that and save and get type error.

76
00:05:36,730 --> 00:05:37,940
It's null stupid.

77
00:05:38,080 --> 00:05:41,090
So you can format these however the hell you want.

78
00:05:41,250 --> 00:05:44,370
All right all right.

79
00:05:44,380 --> 00:05:47,700
So let's see what other types of errors can we do here.

80
00:05:47,710 --> 00:05:51,980
Let's do a syntax error right.

81
00:05:52,060 --> 00:05:56,900
So we'll say it will produce syntax error.

82
00:05:57,130 --> 00:06:02,770
And what I'm going to do here is I'm going to use the Val function which isn't used too much but this

83
00:06:02,770 --> 00:06:05,740
will evaluate javascript within a string.

84
00:06:05,740 --> 00:06:13,480
So for instance we could say two plus two we'd actually have the console log it for it to work or for

85
00:06:13,480 --> 00:06:15,120
it to show us anything.

86
00:06:15,580 --> 00:06:19,270
So you can see we get four so we can evaluate javascript.

87
00:06:19,270 --> 00:06:24,160
Now if we were to do Val and say hello which is going to be look these are going to be looked at as

88
00:06:24,160 --> 00:06:27,310
variables.

89
00:06:28,000 --> 00:06:34,470
If we were to put in here quotes like that the more revaluing evaluating it as a string.

90
00:06:34,600 --> 00:06:41,110
And that doesn't throw an error but if we were to get rid of these double quotes and save that you can

91
00:06:41,110 --> 00:06:44,710
see that there was a syntax error unexpected identifier.

92
00:06:44,710 --> 00:06:48,510
So it's talking about this hello it's looking at it as a variable.

93
00:06:48,520 --> 00:06:48,810
All right.

94
00:06:48,820 --> 00:06:51,380
So that there was a syntax error.

95
00:06:51,590 --> 00:06:56,190
That's another type of error we have is you are I error.

96
00:07:02,820 --> 00:07:03,710
C you are.

97
00:07:03,710 --> 00:07:06,360
I error.

98
00:07:06,800 --> 00:07:10,420
And we could use a function called de-code.

99
00:07:10,430 --> 00:07:16,680
You are a component and then passing something that doesn't make sense.

100
00:07:16,730 --> 00:07:19,850
It's going to pass in a percent sign and save.

101
00:07:19,850 --> 00:07:27,010
And we're going to get an error that says you are by error you are malformed at decoded and component.

102
00:07:27,050 --> 00:07:27,430
All right.

103
00:07:27,440 --> 00:07:31,860
So just just trying to show you the different types of errors that can be thrown.

104
00:07:32,390 --> 00:07:41,270
All right so now that you've seen basically just global errors or core errors being thrown I want to

105
00:07:41,270 --> 00:07:43,590
show you that we can create our own.

106
00:07:43,610 --> 00:07:49,940
So let's say up here we have a variable called user

107
00:07:52,580 --> 00:07:58,980
and let's say there is email say Jadot at gmail dot com.

108
00:07:59,000 --> 00:08:04,370
So let's say we want to check to make sure that this user has a name which it doesn't it just has an

109
00:08:04,370 --> 00:08:05,250
e-mail.

110
00:08:05,450 --> 00:08:10,430
So what we could do is go down here and we could say if

111
00:08:13,280 --> 00:08:19,730
we'll say if no user name then let's throw an error.

112
00:08:19,720 --> 00:08:28,220
So all we have to do is use the throw key word and then we'll say user has no name.

113
00:08:28,510 --> 00:08:35,140
So let's save that and we get down here a user has no name and Cain you could put that inside the dorm

114
00:08:35,170 --> 00:08:39,000
if you wanted to as well and some kind of an alert.

115
00:08:39,010 --> 00:08:44,830
Now another thing you could do this is just throwing the string but you could actually format it as

116
00:08:44,830 --> 00:08:47,280
a certain type of error like a syntax error.

117
00:08:47,290 --> 00:09:00,190
So to do that we could say throw new syntax error and say user has no name and save.

118
00:09:00,400 --> 00:09:02,780
And now you can see we get syntax error.

119
00:09:02,800 --> 00:09:04,170
User has no name.

120
00:09:04,330 --> 00:09:08,910
And it also gives us the line number the file name and line number.

121
00:09:09,520 --> 00:09:11,680
And again you could format this how you want.

122
00:09:11,680 --> 00:09:15,500
You could do something like.

123
00:09:15,900 --> 00:09:18,340
You could say Konsole log

124
00:09:23,370 --> 00:09:24,890
and you can make up your own type.

125
00:09:24,900 --> 00:09:36,760
You could say like user error and then just put in here the dot message and save and we get user error.

126
00:09:36,780 --> 00:09:38,450
User has no name.

127
00:09:38,460 --> 00:09:44,460
All right so gives you a nice elegant way to handle errors and Javascript also shows you the different

128
00:09:44,460 --> 00:09:46,980
types of errors that there are.

129
00:09:46,980 --> 00:09:47,870
All right so that's it.

130
00:09:47,890 --> 00:09:49,810
And I will see you in the next lecture.

