1
00:00:03,270 --> 00:00:10,230
In the previous video, you saw that if the user enters edit and then instead of entering a number,

2
00:00:10,230 --> 00:00:12,130
they enter it to the item.

3
00:00:12,150 --> 00:00:18,810
The program will end abruptly and that is not a good user experience.

4
00:00:18,810 --> 00:00:25,950
So instead of showing this error to the user, instead of stopping the program, we want the program

5
00:00:25,950 --> 00:00:26,960
to continue.

6
00:00:26,970 --> 00:00:33,000
So what we want instead is if the user enters this, we will tell the user that.

7
00:00:34,560 --> 00:00:36,870
They entered the wrong.

8
00:00:37,850 --> 00:00:39,020
Command.

9
00:00:39,200 --> 00:00:45,830
And then we will let the user enter the command again by showing them this prompt.

10
00:00:48,100 --> 00:00:50,830
And that makes for a better user experience.

11
00:00:50,830 --> 00:00:57,280
So never break the programs like that for the user.

12
00:01:00,190 --> 00:01:07,630
So to not let the program and abruptly like this, we will use error handling.

13
00:01:07,630 --> 00:01:09,190
So what is error handling?

14
00:01:09,220 --> 00:01:09,940
Let me show you.

15
00:01:09,940 --> 00:01:10,750
That's to you.

16
00:01:12,550 --> 00:01:16,390
So here we are in the l if added case.

17
00:01:16,600 --> 00:01:21,580
So if the command starts with edit we get.

18
00:01:23,630 --> 00:01:26,150
The number from the commands.

19
00:01:26,150 --> 00:01:34,790
The number is supposed to come after edit, but sometimes the user will enter something else.

20
00:01:34,790 --> 00:01:39,770
So what we want to do is we want to first try out.

21
00:01:40,970 --> 00:01:44,390
We want to try to execute these lines.

22
00:01:44,390 --> 00:01:49,310
So everything under under the live condition, we want to try to execute it.

23
00:01:49,310 --> 00:01:56,630
So what you do is you want to select these lines and then press tab so you indent them.

24
00:01:57,650 --> 00:02:02,210
So now we have eight spaces and what do you do next?

25
00:02:02,210 --> 00:02:08,330
You go here and you press enter and then you say try and I column.

26
00:02:10,070 --> 00:02:20,510
So as you can see, I'm introducing a new code block, just like we had l f blocks, we had width blocks.

27
00:02:20,510 --> 00:02:22,550
We also had the case blocks.

28
00:02:22,550 --> 00:02:28,310
So everything under if under width is intended, the same goes for Troy.

29
00:02:28,310 --> 00:02:30,500
So everything under Troy is indented.

30
00:02:30,500 --> 00:02:35,470
You see all these lines, all of these are indented under trial.

31
00:02:35,600 --> 00:02:43,670
So we try to execute these and if there is an error.

32
00:02:44,520 --> 00:02:53,250
We go here in the same level with try to try is in the same vertical line as you can see and you say

33
00:02:53,250 --> 00:02:58,170
except followed by the error.

34
00:03:00,200 --> 00:03:03,440
The error in our case is value error.

35
00:03:04,520 --> 00:03:12,500
So you go back here and you say value with a capital V error with a capital E

36
00:03:15,260 --> 00:03:16,430
and a column.

37
00:03:22,290 --> 00:03:26,040
So if there is an error, then we want to print out.

38
00:03:29,780 --> 00:03:31,370
Your comment.

39
00:03:33,110 --> 00:03:35,180
It's not valid.

40
00:03:38,620 --> 00:03:45,430
And immediately after that, we want to give the user the opportunity to enter the command again.

41
00:03:46,780 --> 00:03:49,870
So we want to show this message again.

42
00:03:49,870 --> 00:03:54,880
And that message is produced by.

43
00:03:56,580 --> 00:03:59,460
These two lines, so I'll copy them.

44
00:04:00,000 --> 00:04:04,380
Go back here under the print function.

45
00:04:04,590 --> 00:04:09,780
I want to paste them in here and I'll execute the code now.

46
00:04:21,430 --> 00:04:24,610
So type and show edits complete or exit.

47
00:04:26,740 --> 00:04:37,570
Let's say Ed did add a new member presenter and what we get this time is not an error.

48
00:04:37,720 --> 00:04:43,570
It is this message Your command is not valid produced by that line.

49
00:04:43,570 --> 00:04:47,440
And then we have this prompt again.

50
00:04:47,440 --> 00:04:50,590
We can answer again the input.

51
00:04:51,700 --> 00:05:01,120
So if you enter again the wrong command, add a second member or anything like that.

52
00:05:01,340 --> 00:05:05,560
Presenter You get asked again for entering the input.

53
00:05:07,720 --> 00:05:09,010
Now let's try to enter.

54
00:05:09,010 --> 00:05:10,810
Edit one.

55
00:05:13,070 --> 00:05:22,310
So when you enter it, it's one we get this printed out under Troy, you see print number printed out

56
00:05:22,310 --> 00:05:23,270
number one.

57
00:05:23,480 --> 00:05:31,700
And here we enter the new to do add a new player show.

58
00:05:31,700 --> 00:05:35,990
And now as number one we have this adds a new player.

59
00:05:36,590 --> 00:05:39,080
It's working, but it's not ideal.

60
00:05:39,890 --> 00:05:48,950
The ideal solution would be not to have repetitive code because currently these two lines are the same

61
00:05:48,950 --> 00:05:50,930
as these two.

62
00:05:50,960 --> 00:05:54,830
That is not the best code optimization.

63
00:05:55,400 --> 00:06:03,770
So what we can do actually is we use the opposite of a break statement instead of using these.

64
00:06:04,790 --> 00:06:14,460
You remember the break statement breaks the loop and the continuous statement continues.

65
00:06:15,050 --> 00:06:20,210
Or run another cycle of the while loop.

66
00:06:21,530 --> 00:06:23,810
So what's going to happen now is this.

67
00:06:25,160 --> 00:06:27,290
Let's say the user enters.

68
00:06:27,950 --> 00:06:30,200
Edit Add a new member.

69
00:06:31,310 --> 00:06:33,980
The program will try to run these lines.

70
00:06:33,980 --> 00:06:38,900
And if there is an error, then these lines will be executed.

71
00:06:39,410 --> 00:06:45,290
Actually, if there is a value error, because there are different kinds of errors out there, it is

72
00:06:45,290 --> 00:06:52,310
important to specify the error here because you don't want to get confused with all kinds of errors.

73
00:06:52,310 --> 00:06:56,090
So you want to execute these only when this error occurs.

74
00:06:56,690 --> 00:07:01,010
So when that error occurs, you print out the message.

75
00:07:02,630 --> 00:07:03,800
In the command line.

76
00:07:03,800 --> 00:07:14,510
And then this statement here, what it will do is it will ignore everything that comes underneath here

77
00:07:14,660 --> 00:07:20,600
and it will go back again to the beginning of the loop here in line two.

78
00:07:21,230 --> 00:07:24,350
So it will execute this input function.

79
00:07:28,510 --> 00:07:29,560
Let's try it.

80
00:07:31,240 --> 00:07:32,380
Run the code.

81
00:07:34,130 --> 00:07:34,870
Edit.

82
00:07:37,630 --> 00:07:39,430
Add a new member.

83
00:07:40,600 --> 00:07:42,640
Your command is not valid.

84
00:07:42,670 --> 00:07:49,900
Type ads show edits, complete or exit edit Add a new member.

85
00:07:50,530 --> 00:07:53,410
Again we get your command is not valid.

86
00:07:54,250 --> 00:08:08,320
Type add show, edit or complete or exit edit to enter a new to do Update the OS press Enter show and

87
00:08:08,320 --> 00:08:11,410
update The OS is number two now.

88
00:08:17,490 --> 00:08:21,330
So that was an example of the tri accept block.

89
00:08:24,100 --> 00:08:30,880
Before I close the video, I want to create another tri accept block here in this code because we have

90
00:08:30,880 --> 00:08:36,880
another error here which might happen in the complete condition.

91
00:08:39,390 --> 00:08:43,919
Because if we run the code and the user tries to complete.

92
00:08:47,950 --> 00:08:51,040
A night in which is not in the list.

93
00:08:51,040 --> 00:08:58,840
For example, item 23, when all we have is a few items and in that case we will get an index error.

94
00:08:59,020 --> 00:09:01,810
It says that the list index is out of range.

95
00:09:06,210 --> 00:09:12,900
So that's the same error as if we had a least.

96
00:09:16,180 --> 00:09:27,010
For example, A and B, So with two items and you try to access item with index 23 or 24, so you will

97
00:09:27,010 --> 00:09:28,900
get that error again, index error.

98
00:09:29,770 --> 00:09:37,900
So let's quickly handle this error as well so that the user knows that he is out of the range of the

99
00:09:37,900 --> 00:09:38,680
items.

100
00:09:40,680 --> 00:09:43,860
Available in the list in the to do list.

101
00:09:46,540 --> 00:09:51,100
So again, we want to select all of these.

102
00:09:51,460 --> 00:09:58,510
So all the lines which are under live and we want to indent them by pressing tab and then we want to

103
00:09:58,510 --> 00:09:59,710
add try here.

104
00:10:01,360 --> 00:10:04,900
So we will try to execute all that code and then.

105
00:10:08,980 --> 00:10:12,310
Catch an index error.

106
00:10:12,310 --> 00:10:16,570
So that error there index.

107
00:10:19,020 --> 00:10:19,620
Error.

108
00:10:22,650 --> 00:10:25,050
So if that's the case, we want to print out.

109
00:10:26,860 --> 00:10:30,640
There is no item with that number.

110
00:10:34,240 --> 00:10:36,700
And then continue again.

111
00:10:36,850 --> 00:10:43,690
So which means that if the user enters a number which is out of range, we show that message and then

112
00:10:43,690 --> 00:10:46,810
we let the user enter.

113
00:10:47,980 --> 00:10:49,300
A new commands again.

114
00:10:49,300 --> 00:10:52,750
So continue will execute these lines again.

115
00:10:53,890 --> 00:11:00,600
So now if the user enters complete 23, they get this message.

116
00:11:02,610 --> 00:11:06,060
And they get the chance to enter command again.

117
00:11:07,260 --> 00:11:10,530
So that was the lecture about error handling.

118
00:11:11,710 --> 00:11:12,670
Let's keep going.

