1
00:00:00,810 --> 00:00:02,160
So in the last lesson,

2
00:00:02,190 --> 00:00:07,190
we looked at how we can use this JSON format to save our data and to load

3
00:00:09,120 --> 00:00:11,070
our data into our program.

4
00:00:11,730 --> 00:00:16,730
But one of the problems that we have is if we run our program from scratch

5
00:00:17,280 --> 00:00:21,600
where we don't have that data.json file created,

6
00:00:21,930 --> 00:00:26,930
then the first thing that it's going to encounter once it lands inside here is

7
00:00:27,120 --> 00:00:30,570
it's going to try and open a file that doesn't exist.

8
00:00:31,110 --> 00:00:34,050
And what happens then? We get a file

9
00:00:34,050 --> 00:00:38,490
not found error because there is no such file data.json.

10
00:00:39,030 --> 00:00:41,400
So what can we do? Well,

11
00:00:41,640 --> 00:00:46,640
we already learned about dealing with exceptions and handling them so that we

12
00:00:47,160 --> 00:00:49,380
don't end up in these situations.

13
00:00:49,980 --> 00:00:54,980
So now it's time to put your knowledge into practice inside a real project.

14
00:00:55,650 --> 00:01:00,650
And I want you to think about how you can catch that particular exception,

15
00:01:01,380 --> 00:01:06,060
how you can get it to work so that if this fails,

16
00:01:06,330 --> 00:01:09,780
then we simply create a brand new data file.

17
00:01:10,200 --> 00:01:13,530
But if it does work, then we can simply load up the data,

18
00:01:13,860 --> 00:01:16,050
update the data and write the data.

19
00:01:16,680 --> 00:01:19,830
So the actual implementation I'll leave up to you.

20
00:01:20,220 --> 00:01:25,220
But what you want to be able to do is to not have a data.json file and run

21
00:01:26,190 --> 00:01:28,380
your code, and it would not crash

22
00:01:28,500 --> 00:01:33,500
and instead it would create a brand new data.json file with the new data.

23
00:01:35,640 --> 00:01:40,640
But if you had already got some old data, then it should simply just update and

24
00:01:40,770 --> 00:01:45,600
write it to file. Pause the video and see if you can complete this

25
00:01:45,720 --> 00:01:46,590
as a challenge.

26
00:01:50,970 --> 00:01:55,200
This always starts with analyzing which part of our code is the part that's

27
00:01:55,200 --> 00:01:59,400
going to fail or likely to fail at some point. And in our case,

28
00:01:59,460 --> 00:02:00,960
it's this line right here,

29
00:02:01,140 --> 00:02:06,140
opening this line in read mode is dangerous because if this doesn't exist,

30
00:02:07,260 --> 00:02:09,720
then we get that file not found error.

31
00:02:10,259 --> 00:02:15,090
So what we can do is we can put this inside a try statement.

32
00:02:15,660 --> 00:02:20,010
Try opening this file and try reading the data that's inside.

33
00:02:20,790 --> 00:02:24,630
Now, if this fails, however, then we can catch it

34
00:02:25,110 --> 00:02:28,230
and we're going to catch a very specific type of exception,

35
00:02:28,260 --> 00:02:32,250
which is the file not found error exception. Now,

36
00:02:32,250 --> 00:02:34,050
when this exception happens,

37
00:02:34,230 --> 00:02:38,490
what we want to do is we want to create a new file

38
00:02:38,820 --> 00:02:43,740
which is going to be our data.json. So at this point,

39
00:02:43,740 --> 00:02:48,090
remember it doesn't yet exist. And we're going to open it in write mode.

40
00:02:48,750 --> 00:02:53,490
And this data file is going to be used so that we can write to it

41
00:02:53,520 --> 00:02:57,810
and we can dump our newly created data up here.

42
00:02:58,350 --> 00:03:01,480
Remember, when the user hits the add button,

43
00:03:01,780 --> 00:03:06,190
then everything they've typed in these entries gets aggregated into this

44
00:03:06,190 --> 00:03:07,023
dictionary

45
00:03:07,210 --> 00:03:11,710
and that dictionary is what we want to dump into this new data file.

46
00:03:12,340 --> 00:03:16,900
So we'll get hold of that new data and we'll dump it into the data file.

47
00:03:17,830 --> 00:03:18,550
And again,

48
00:03:18,550 --> 00:03:23,550
we'll keep the indent to four spaces just so that we can read it more easily.

49
00:03:25,030 --> 00:03:28,750
Now this is what should happen if the file's not found.

50
00:03:29,380 --> 00:03:33,250
But if the file was found and it already exists,

51
00:03:33,340 --> 00:03:37,240
then we're going to tap into the else statement. And in the else statement

52
00:03:37,270 --> 00:03:42,070
what we're going to do is we're going to get hold of the data which we managed

53
00:03:42,070 --> 00:03:46,270
to get because this block of code succeeded, and in the else block

54
00:03:46,270 --> 00:03:50,950
what we're going to do is we're going to update the data with the new data. Now,

55
00:03:51,100 --> 00:03:53,500
this else block is only going to be triggered

56
00:03:53,530 --> 00:03:56,650
if everything inside the try block was successful.

57
00:03:57,070 --> 00:03:59,260
So that means by the end of this block,

58
00:03:59,290 --> 00:04:04,290
we have this data which holds onto all of the existing data from that data file

59
00:04:05,020 --> 00:04:09,670
and we update that data with the new data which is this dictionary here.

60
00:04:10,360 --> 00:04:13,210
Now, after we've updated that data,

61
00:04:13,270 --> 00:04:17,560
we want to be able to open up data.jason in the write mode

62
00:04:17,950 --> 00:04:22,950
and then dump this updated data from here into that data file.

63
00:04:25,480 --> 00:04:27,940
And then once all of this is done,

64
00:04:28,570 --> 00:04:32,980
no matter if there was an exception or if there was no issues,

65
00:04:33,280 --> 00:04:37,840
we're going to still need to delete everything that's inside the website and

66
00:04:37,840 --> 00:04:38,800
password entry.

67
00:04:39,220 --> 00:04:43,240
So we can even add a finally keyword right here.

68
00:04:44,560 --> 00:04:49,360
So this is the final code that deals with our exception.

69
00:04:49,690 --> 00:04:54,460
We have our try block which can fail, we have our except block

70
00:04:54,490 --> 00:04:58,720
which deals with any failures that occur, we have our else block

71
00:04:58,780 --> 00:05:03,010
which has the code that needs to be run if there were no issues,

72
00:05:03,370 --> 00:05:07,810
and finally, at the very end no matter if there was an issue or no issue,

73
00:05:08,140 --> 00:05:12,640
we have a block of code which clears our website and password entries.

74
00:05:13,240 --> 00:05:16,660
Now you might've noticed there's a little bit of repetition here with the

75
00:05:16,690 --> 00:05:19,090
writing to file. So if you want to,

76
00:05:19,120 --> 00:05:22,660
you could in fact create a separate method somewhere up here.

77
00:05:23,110 --> 00:05:26,980
But I'm actually going to keep this as it is just because I think when you look

78
00:05:26,980 --> 00:05:30,550
at this code, if you had any issues and you wanted to review it,

79
00:05:31,060 --> 00:05:35,920
this is much easier to understand and reason about compared to a separate

80
00:05:35,920 --> 00:05:36,753
function.

81
00:05:37,330 --> 00:05:40,930
Feel free to refactor this code and make it as short as you want.

82
00:05:41,380 --> 00:05:43,210
But this is the version I'm going to keep

83
00:05:43,450 --> 00:05:46,690
just so that if you had any issues and you want to take a look at it,

84
00:05:46,960 --> 00:05:47,860
it's there for you

85
00:05:47,950 --> 00:05:52,950
and it's easy to see the try, except, else, and finally blocks with all of the

86
00:05:54,070 --> 00:05:55,660
code together in one place.

87
00:05:58,130 --> 00:05:59,330
Now in the next lesson,

88
00:05:59,390 --> 00:06:02,750
we're going to be completing the functionality of our password manager.

89
00:06:03,110 --> 00:06:06,380
We're gonna add that all important search functionality

90
00:06:06,740 --> 00:06:11,510
which is going to look through our JSON, fetch the results for a particular

91
00:06:11,510 --> 00:06:14,630
website, and then show it to us in a popup.

92
00:06:15,050 --> 00:06:18,380
So for all of that and more, I'll see you on the next lesson.

