1
00:00:00,600 --> 00:00:01,500
Instructor: In this exercise,

2
00:00:01,500 --> 00:00:03,990
we're going to write some exception handling code

3
00:00:03,990 --> 00:00:07,890
to handle a different type of exception, a KeyError.

4
00:00:07,890 --> 00:00:10,380
When you hear keys, you're probably thinking dictionaries

5
00:00:10,380 --> 00:00:11,910
and you would be right.

6
00:00:11,910 --> 00:00:13,980
So in this case, in the input area,

7
00:00:13,980 --> 00:00:15,780
you can see we have a list

8
00:00:15,780 --> 00:00:18,390
of dictionaries that contains some data

9
00:00:18,390 --> 00:00:20,400
on some Facebook posts.

10
00:00:20,400 --> 00:00:23,040
As you may know, Facebook posts can have likes,

11
00:00:23,040 --> 00:00:24,870
they can have comments, they can have shares,

12
00:00:24,870 --> 00:00:27,840
they can have lots of different attributes assigned to them

13
00:00:27,840 --> 00:00:30,660
and this can be collected in a dictionary.

14
00:00:30,660 --> 00:00:34,161
Now, the problem occurs on line seven of the starting code

15
00:00:34,161 --> 00:00:38,820
where we try to access, in each of the dictionaries,

16
00:00:38,820 --> 00:00:43,820
the posts, the number of likes that that post has.

17
00:00:44,340 --> 00:00:46,410
Now, the problem here is that if you look

18
00:00:46,410 --> 00:00:49,530
at the input or if you look at the Facebook posts

19
00:00:49,530 --> 00:00:52,650
in a better structured way in the description pane,

20
00:00:52,650 --> 00:00:54,420
you'll see that some of those posts,

21
00:00:54,420 --> 00:00:58,260
some of those dictionaries don't have a value for likes.

22
00:00:58,260 --> 00:01:00,450
Those posts didn't get liked at all.

23
00:01:00,450 --> 00:01:04,800
They were hated on, or they had different reactions.

24
00:01:04,800 --> 00:01:07,680
But either way, if we try to access a key

25
00:01:07,680 --> 00:01:10,275
from a dictionary that doesn't exist, we'll end up

26
00:01:10,275 --> 00:01:12,840
with a KeyError and you'll see

27
00:01:12,840 --> 00:01:14,640
that if you try to run the code

28
00:01:14,640 --> 00:01:17,520
without changing the starting code at all.

29
00:01:17,520 --> 00:01:20,670
Now, the objective is for you to use what you've learned

30
00:01:20,670 --> 00:01:22,080
about exception handling

31
00:01:22,080 --> 00:01:24,513
to prevent the program from crashing.

32
00:01:25,380 --> 00:01:28,980
And instead, we're going to print out the total number

33
00:01:28,980 --> 00:01:31,830
of likes of this user's Facebook posts

34
00:01:31,830 --> 00:01:36,830
and we're going to skip the ones where there are no likes

35
00:01:37,020 --> 00:01:39,360
for that particular post.

36
00:01:39,360 --> 00:01:42,330
Take a look at the description pane, read through it,

37
00:01:42,330 --> 00:01:43,980
take a look at the starting code

38
00:01:43,980 --> 00:01:45,360
and see if you can figure out

39
00:01:45,360 --> 00:01:48,243
how to make all the tests pass in this exercise.

40
00:01:53,910 --> 00:01:55,770
The first thing we do, as always,

41
00:01:55,770 --> 00:02:00,770
is to put the problematic code inside a little jail,

42
00:02:00,810 --> 00:02:02,490
a try jail.

43
00:02:02,490 --> 00:02:05,850
And in this case, we have inside the for loop,

44
00:02:05,850 --> 00:02:08,550
we have another block, which is the try block.

45
00:02:08,550 --> 00:02:12,270
Inside the try block, we have our offending line of code,

46
00:02:12,270 --> 00:02:15,870
total_likes equals total_likes plus post,

47
00:02:15,870 --> 00:02:18,480
accessing each of those dictionaries,

48
00:02:18,480 --> 00:02:22,923
which we've called posts and accessing it by the key likes.

49
00:02:23,760 --> 00:02:26,430
Now, this is the line that we know causes the error

50
00:02:26,430 --> 00:02:30,180
and this is the line that we're going to try to execute.

51
00:02:30,180 --> 00:02:33,240
What happens if our try fails?

52
00:02:33,240 --> 00:02:36,570
Well, we're going to add an accept block,

53
00:02:36,570 --> 00:02:38,160
and we're going to define the accept

54
00:02:38,160 --> 00:02:40,740
as a KeyError exception.

55
00:02:40,740 --> 00:02:44,970
Writing accept KeyError: means that if a KeyError

56
00:02:44,970 --> 00:02:48,060
does occur when we try to run line seven,

57
00:02:48,060 --> 00:02:53,060
then well, action is going to skip that line, and instead,

58
00:02:53,070 --> 00:02:56,400
we're going to run pass, which inside a loop

59
00:02:56,400 --> 00:02:59,280
is going to skip over this line

60
00:02:59,280 --> 00:03:02,280
and go to the next item in the loop.

61
00:03:02,280 --> 00:03:04,415
So we're going to loop through all

62
00:03:04,415 --> 00:03:08,670
of the dictionaries in our list called facebook_posts.

63
00:03:08,670 --> 00:03:11,730
If one of them doesn't have any likes,

64
00:03:11,730 --> 00:03:15,090
we pass on that one and we continue with the next one.

65
00:03:15,090 --> 00:03:19,290
And then at the end, we end up accumulating our total number

66
00:03:19,290 --> 00:03:21,540
of likes and we print it out,

67
00:03:21,540 --> 00:03:25,350
and that is the solution to this challenge.

68
00:03:25,350 --> 00:03:27,870
Exception handling is incredibly effective

69
00:03:27,870 --> 00:03:29,370
and incredibly useful,

70
00:03:29,370 --> 00:03:32,790
although it is sometimes neglected by developers

71
00:03:32,790 --> 00:03:36,660
but by hopefully learning more exceptions and thinking

72
00:03:36,660 --> 00:03:39,360
about exception handling using try/accept blocks,

73
00:03:39,360 --> 00:03:42,450
you're getting to grips around a really key part

74
00:03:42,450 --> 00:03:45,933
of development, which is error and exception handling.

