1
1

00:00:00,380  -->  00:00:01,940
<v ->Hi guys and welcome back!</v>
2

2

00:00:01,940  -->  00:00:03,120
In this video, we're going to learn
3

3

00:00:03,120  -->  00:00:06,513
about Custom Error Classes and how they can be helpful.
4

4

00:00:08,140  -->  00:00:09,658
Let's say we've got a class book
5

5

00:00:09,658  -->  00:00:13,760
that defines an init method and sets three properties,
6

6

00:00:13,760  -->  00:00:16,780
the name, the page count, and the pages read.
7

7

00:00:16,780  -->  00:00:19,120
Note that I have used type hinting here
8

8

00:00:19,120  -->  00:00:21,150
to tell me that the name should be a string
9

9

00:00:21,150  -->  00:00:23,150
and the page count should be an integer.
10

10

00:00:24,660  -->  00:00:27,040
I'm going to also add a couple other methods here
11

11

00:00:27,040  -->  00:00:29,600
just to review what we've already learned.
12

12

00:00:29,600  -->  00:00:33,200
The repr method that we return some information
13

13

00:00:33,200  -->  00:00:36,451
about the book in a format that will allow me
14

14

00:00:36,451  -->  00:00:38,560
to then recreate this book,
15

15

00:00:38,560  -->  00:00:41,260
so it contains all of the information about this book.
16

16

00:00:42,580  -->  00:00:44,800
And by the way, you can put a return statement
17

17

00:00:44,800  -->  00:00:48,300
in multiple lines by using brackets and like that
18

18

00:00:48,300  -->  00:00:50,190
if you want to return a longer string
19

19

00:00:50,190  -->  00:00:52,390
and you don't want your code to be too long.
20

20

00:00:53,860  -->  00:00:54,790
Something like that.
21

21

00:00:54,790  -->  00:00:56,010
If I put it in a single line,
22

22

00:00:56,010  -->  00:00:58,100
it would be too long and I would have to scroll
23

23

00:00:58,100  -->  00:00:59,100
in order to read it.
24

24

00:01:01,340  -->  00:01:03,250
And I'm also going to make a read function
25

25

00:01:03,250  -->  00:01:06,590
that allows me to read a bunch of pages.
26

26

00:01:06,590  -->  00:01:09,370
So I will allow it to take in that argument
27

27

00:01:09,370  -->  00:01:12,970
and say self.pages read plus equal pages.
28

28

00:01:12,970  -->  00:01:15,167
And then I will print out a message saying,
29

29

00:01:15,167  -->  00:01:18,747
"You have now read that number of pages."
30

30

00:01:20,320  -->  00:01:21,240
Perfect!
31

31

00:01:21,240  -->  00:01:24,450
Note this is a simple book class that allows me
32

32

00:01:24,450  -->  00:01:25,530
to create new books.
33

33

00:01:25,530  -->  00:01:29,830
For example, Python 101 can be a book called "Python 101"
34

34

00:01:29,830  -->  00:01:31,520
and it may have 50 pages.
35

35

00:01:31,520  -->  00:01:34,660
And then I can do python101.read 35
36

36

00:01:36,160  -->  00:01:38,000
and then I'm going to also read 50 pages.
37

37

00:01:38,000  -->  00:01:39,890
So clearly, you can't do that, right?
38

38

00:01:39,890  -->  00:01:43,760
Because it only has 50 pages, you can't read 85 pages.
39

39

00:01:43,760  -->  00:01:45,733
But what happens if we press play?
40

40

00:01:47,600  -->  00:01:50,580
Well you have now read 35 pages out of 50
41

41

00:01:50,580  -->  00:01:53,689
and then you have now read 85 pages out of 50.
42

42

00:01:53,689  -->  00:01:55,340
Not great!
43

43

00:01:55,340  -->  00:01:57,850
So we are going to use arrows instead
44

44

00:01:57,850  -->  00:02:00,920
in order to prevent that from happening.
45

45

00:02:00,920  -->  00:02:03,820
Note here in the read method,
46

46

00:02:03,820  -->  00:02:06,420
just before we add anything to pages read,
47

47

00:02:06,420  -->  00:02:10,290
we're gonna say if self.pages read plus pages
48

48

00:02:10,290  -->  00:02:11,950
is greater than self.page,
49

49

00:02:13,770  -->  00:02:15,767
then we're going to raise an error and we're gonna say,
50

50

00:02:15,767  -->  00:02:19,390
"Too many pages read error."
51

51

00:02:19,390  -->  00:02:21,380
And in here, we're going to put in the message
52

52

00:02:21,380  -->  00:02:23,563
that will say something like this.
53

53

00:02:25,410  -->  00:02:28,220
Of course, this error is not built into Python
54

54

00:02:28,220  -->  00:02:31,410
because it's a bit too specific for our programme logic.
55

55

00:02:31,410  -->  00:02:34,670
We could use something like value error.
56

56

00:02:34,670  -->  00:02:38,630
After all, this page is the wrong amount
57

57

00:02:38,630  -->  00:02:40,610
given the context of our programme
58

58

00:02:40,610  -->  00:02:42,330
but it's not really specific enough.
59

59

00:02:42,330  -->  00:02:44,770
So when we are getting an error back
60

60

00:02:44,770  -->  00:02:46,760
in our console, it may be helpful to have
61

61

00:02:46,760  -->  00:02:48,163
a slightly nicer name.
62

62

00:02:49,090  -->  00:02:52,040
Note what we can do is before the class there,
63

63

00:02:52,040  -->  00:02:53,360
I usually like to define my errors
64

64

00:02:53,360  -->  00:02:55,060
at the top of the file, but it doesn't really matter.
65

65

00:02:55,060  -->  00:02:57,123
You can put them at the bottom if you want.
66

66

00:02:57,123  -->  00:03:01,070
We can say, "Too many pages read error,"
67

67

00:03:01,070  -->  00:03:05,550
and we're gonna make it inherit from value error.
68

68

00:03:05,550  -->  00:03:08,130
You can inherit from any built-in exception or error
69

69

00:03:08,130  -->  00:03:11,890
if you like, in order to make it an exception class.
70

70

00:03:11,890  -->  00:03:16,890
If you don't inherit from an error or the exception class
71

71

00:03:17,100  -->  00:03:19,360
then you won't be able to raise it
72

72

00:03:19,360  -->  00:03:21,810
because these classes have some methods inside them
73

73

00:03:21,810  -->  00:03:25,210
that you need in order to be able to raise them as errors.
74

74

00:03:25,210  -->  00:03:27,360
Note you can create that and you actually don't have
75

75

00:03:27,360  -->  00:03:29,900
to do anything inside it because it already extend
76

76

00:03:29,900  -->  00:03:32,510
a message and all that, so you can just put pass.
77

77

00:03:32,510  -->  00:03:34,624
And this too many pages read error
78

78

00:03:34,624  -->  00:03:37,000
will essentially just be a copy of value error
79

79

00:03:37,000  -->  00:03:38,890
but with a different name.
80

80

00:03:38,890  -->  00:03:40,530
Note now that when we raise this error,
81

81

00:03:40,530  -->  00:03:42,870
we're going to get a nice message there
82

82

00:03:42,870  -->  00:03:45,187
that says, "Too many pages read error.
83

83

00:03:45,187  -->  00:03:46,457
"You tried to read 85 pages,
84

84

00:03:46,457  -->  00:03:48,307
"but this book only has 50 pages."
85

85

00:03:49,820  -->  00:03:52,250
In addition, you get your traceback what tells you
86

86

00:03:52,250  -->  00:03:54,300
where the problem was, which is that you tried
87

87

00:03:54,300  -->  00:03:58,010
to read 50 pages there but you'd already read some before.
88

88

00:03:58,010  -->  00:03:59,440
When you're looking back through your code,
89

89

00:03:59,440  -->  00:04:02,230
you can very easily determine where abouts
90

90

00:04:02,230  -->  00:04:03,290
in your code the problem happened
91

91

00:04:03,290  -->  00:04:05,030
and you can read through it and try to find out
92

92

00:04:05,030  -->  00:04:06,093
what the problem was.
93

93

00:04:07,140  -->  00:04:09,770
Now of course, this isn't great for users,
94

94

00:04:09,770  -->  00:04:12,440
so if you wanted to print a nicer message,
95

95

00:04:12,440  -->  00:04:15,983
you can always try to do that.
96

96

00:04:18,489  -->  00:04:22,527
And if not, except it, and print the message on its own.
97

97

00:04:25,110  -->  00:04:25,943
Just like that.
98

98

00:04:28,830  -->  00:04:30,630
That's it for creating custom classes.
99

99

00:04:30,630  -->  00:04:32,510
It's really very straightforward.
100

100

00:04:32,510  -->  00:04:34,070
You just inherit from the most
101

101

00:04:34,070  -->  00:04:36,220
relevant built-in error class,
102

102

00:04:36,220  -->  00:04:39,400
like value error, runtime error, or even type error
103

103

00:04:39,400  -->  00:04:41,420
and you name it as you would like.
104

104

00:04:41,420  -->  00:04:43,490
That gives you a slightly nicer name
105

105

00:04:43,490  -->  00:04:45,650
and the ability to find your problems
106

106

00:04:45,650  -->  00:04:46,870
a bit more easily.
107

107

00:04:46,870  -->  00:04:48,610
Most of the time, you don't have to put anything
108

108

00:04:48,610  -->  00:04:49,790
inside the error classes,
109

109

00:04:49,790  -->  00:04:52,080
just giving them a new name is good enough.
110

110

00:04:52,080  -->  00:04:54,350
But you can do, if you want.
111

111

00:04:54,350  -->  00:04:57,070
We're not gonna cover how to populate the body
112

112

00:04:57,070  -->  00:04:59,140
of your custom error classes in this course, though.
113

113

00:04:59,140  -->  00:05:00,890
That's a little bit more advanced.
114

114

00:05:00,890  -->  00:05:02,330
Thank you for joining me in this one
115

115

00:05:02,330  -->  00:05:04,080
and I'll see you in the next video.
