1
00:00:00,840 --> 00:00:02,160
Instructor: In this exercise,

2
00:00:02,160 --> 00:00:04,920
we're going to bake some pies.

3
00:00:04,920 --> 00:00:07,770
If you take a look at the starting code,

4
00:00:07,770 --> 00:00:12,770
you'll see that we're taking the input as a list of fruits

5
00:00:13,140 --> 00:00:16,379
and we're putting it into our code,

6
00:00:16,379 --> 00:00:21,380
and on line 11, we call the function make pie

7
00:00:22,410 --> 00:00:27,410
and tell it which pie out of our list of fruits to make.

8
00:00:27,840 --> 00:00:32,790
So if make pie zero, then that would be to make apple pie,

9
00:00:32,790 --> 00:00:35,310
make pie one, that should be a pear pie,

10
00:00:35,310 --> 00:00:37,830
make pie two, that should be an orange pie.

11
00:00:37,830 --> 00:00:40,530
Now, unfortunately, we are providing a index

12
00:00:40,530 --> 00:00:44,820
that is way out of range of our list of fruits.

13
00:00:44,820 --> 00:00:46,230
It's like we're trying to make a pie

14
00:00:46,230 --> 00:00:49,200
with a fruit type that doesn't exist.

15
00:00:49,200 --> 00:00:51,052
This normally would crash our code,

16
00:00:51,052 --> 00:00:54,450
and indeed, if you just try running the code as it is

17
00:00:54,450 --> 00:00:55,770
with the starting code,

18
00:00:55,770 --> 00:01:00,000
you will see the errors in the output area.

19
00:01:00,000 --> 00:01:04,709
Your job is to update the make pie function

20
00:01:04,709 --> 00:01:06,810
so that in the case where the user

21
00:01:06,810 --> 00:01:09,390
does something silly like this on line 11

22
00:01:09,390 --> 00:01:12,600
where they try to pick a fruit that does not exist

23
00:01:12,600 --> 00:01:14,580
in our list of fruits,

24
00:01:14,580 --> 00:01:17,010
then instead of crashing the program,

25
00:01:17,010 --> 00:01:19,710
we're just going to print "fruit pie".

26
00:01:19,710 --> 00:01:21,690
So we don't know what fruit you want,

27
00:01:21,690 --> 00:01:24,210
we're just gonna tell you we just made some fruit pie

28
00:01:24,210 --> 00:01:26,490
and tell the user to go away.

29
00:01:26,490 --> 00:01:28,770
But this is a really important skill,

30
00:01:28,770 --> 00:01:31,050
the way that we can handle errors

31
00:01:31,050 --> 00:01:35,430
instead of allowing it to crash our application.

32
00:01:35,430 --> 00:01:37,380
Take a look in the description pane,

33
00:01:37,380 --> 00:01:39,300
look at the example input

34
00:01:39,300 --> 00:01:42,750
and what the example output is expected,

35
00:01:42,750 --> 00:01:47,223
and write some code to catch this exception.

36
00:01:48,060 --> 00:01:48,893
Have a go now.

37
00:01:56,340 --> 00:01:59,760
The first thing we do is using a try block,

38
00:01:59,760 --> 00:02:01,260
we catch the line of code

39
00:02:01,260 --> 00:02:05,880
that is potentially going to cause us an error,

40
00:02:05,880 --> 00:02:09,150
and in this case, it is the line on line seven

41
00:02:09,150 --> 00:02:12,150
where we try to access the list of fruits

42
00:02:12,150 --> 00:02:15,630
at an index that may or may not exist.

43
00:02:15,630 --> 00:02:18,930
When we call line 13, make pie four,

44
00:02:18,930 --> 00:02:21,810
we pass four into line seven

45
00:02:21,810 --> 00:02:26,070
and we try to access our list of fruits at index four.

46
00:02:26,070 --> 00:02:27,750
So that obviously doesn't exist.

47
00:02:27,750 --> 00:02:29,520
Index three doesn't even exist.

48
00:02:29,520 --> 00:02:32,340
There's only three items in our list.

49
00:02:32,340 --> 00:02:34,230
So this line, line seven,

50
00:02:34,230 --> 00:02:36,840
is the one that causes the index error,

51
00:02:36,840 --> 00:02:38,580
and that's the one that we are penning in

52
00:02:38,580 --> 00:02:41,463
in this safe environment under try.

53
00:02:43,050 --> 00:02:46,830
The next part is where we have our exception.

54
00:02:46,830 --> 00:02:48,750
We know from running the code,

55
00:02:48,750 --> 00:02:51,870
the type of exception we get is an index error,

56
00:02:51,870 --> 00:02:55,320
and this is the exception that we wanna handle in this case.

57
00:02:55,320 --> 00:02:57,780
Now, there can be many, many other types

58
00:02:57,780 --> 00:02:59,310
of errors and exceptions.

59
00:02:59,310 --> 00:03:02,040
We don't want to handle all of them in this way

60
00:03:02,040 --> 00:03:03,150
because there might be things

61
00:03:03,150 --> 00:03:05,580
that we haven't even predicted that's wrong.

62
00:03:05,580 --> 00:03:07,950
The only thing that we know that could go wrong

63
00:03:07,950 --> 00:03:10,260
that we wanna handle is the index error.

64
00:03:10,260 --> 00:03:12,870
So we have except index error,

65
00:03:12,870 --> 00:03:14,880
and when this error does happen,

66
00:03:14,880 --> 00:03:17,520
we're going to print fruit pie

67
00:03:17,520 --> 00:03:19,983
instead of crashing our application.

68
00:03:21,690 --> 00:03:23,580
Finally, we have an else block

69
00:03:23,580 --> 00:03:27,450
that catches the case where there are no exceptions,

70
00:03:27,450 --> 00:03:30,120
and in this case, we're just going to do our usual,

71
00:03:30,120 --> 00:03:33,810
which is to print the name of the fruit from the list

72
00:03:33,810 --> 00:03:36,153
that is picked plus the word pie.

73
00:03:37,050 --> 00:03:40,290
And this block of code now in make pie

74
00:03:40,290 --> 00:03:44,700
is now safe against the potential index error.

75
00:03:44,700 --> 00:03:46,854
If the user changes line 13

76
00:03:46,854 --> 00:03:49,860
to say make pie one, make pie two,

77
00:03:49,860 --> 00:03:52,410
it's going to function as expected,

78
00:03:52,410 --> 00:03:55,500
printing out the name of the fruit and the word pie,

79
00:03:55,500 --> 00:03:57,060
but if they make a mistake

80
00:03:57,060 --> 00:04:00,900
and happen to go out of the range, of the list,

81
00:04:00,900 --> 00:04:03,330
then we're just going to print fruit pie.

82
00:04:03,330 --> 00:04:06,270
There's no crashes, there's no alarm bells,

83
00:04:06,270 --> 00:04:09,900
everything is all safe and all handled,

84
00:04:09,900 --> 00:04:12,960
which is the point of our exception handling.

85
00:04:12,960 --> 00:04:14,700
And this is a really important skill,

86
00:04:14,700 --> 00:04:17,370
if not a little bit neglected by developers

87
00:04:17,370 --> 00:04:19,890
because it's not fun thinking about edge cases

88
00:04:19,890 --> 00:04:21,630
and handling them each time.

89
00:04:21,630 --> 00:04:23,970
But this is an important skill on your road

90
00:04:23,970 --> 00:04:26,073
to becoming a professional developer.

