-: Hi guys and welcome back!
In this video, we're going to learn
about Custom Error Classes and how they can be helpful.
Let's say we've got a class book
that defines an init method and sets three properties,
the name, the page count, and the pages read.
Note that I have used type hinting here
to tell me that the name should be a string
and the page count should be an integer.
I'm going to also add a couple other methods here
just to review what we've already learned.
The repr method that we return some information
about the book in a format that will allow me
to then recreate this book,
so it contains all of the information about this book.
And by the way, you can put a return statement
in multiple lines by using brackets and like that
if you want to return a longer string
and you don't want your code to be too long.
Something like that.
If I put it in a single line,
it would be too long and I would have to scroll
in order to read it.
And I'm also going to make a read function
that allows me to read a bunch of pages.
So I will allow it to take in that argument
and say self.pages read plus equal pages.
And then I will print out a message saying,
"You have now read that number of pages."
Perfect!
Note this is a simple book class that allows me
to create new books.
For example, Python 101 can be a book called "Python 101"
and it may have 50 pages.
And then I can do python101.read 35
and then I'm going to also read 50 pages.
So clearly, you can't do that, right?
Because it only has 50 pages, you can't read 85 pages.
But what happens if we press play?
Well you have now read 35 pages out of 50
and then you have now read 85 pages out of 50.
Not great!
So we are going to use arrows instead
in order to prevent that from happening.
Note here in the read method,
just before we add anything to pages read,
we're gonna say if self.pages read plus pages
is greater than self.page,
then we're going to raise an error and we're gonna say,
"Too many pages read error."
And in here, we're going to put in the message
that will say something like this.
Of course, this error is not built into Python
because it's a bit too specific for our programme logic.
We could use something like value error.
After all, this page is the wrong amount
given the context of our programme
but it's not really specific enough.
So when we are getting an error back
in our console, it may be helpful to have
a slightly nicer name.
Note what we can do is before the class there,
I usually like to define my errors
at the top of the file, but it doesn't really matter.
You can put them at the bottom if you want.
We can say, "Too many pages read error,"
and we're gonna make it inherit from value error.
You can inherit from any built-in exception or error
if you like, in order to make it an exception class.
If you don't inherit from an error or the exception class
then you won't be able to raise it
because these classes have some methods inside them
that you need in order to be able to raise them as errors.
Note you can create that and you actually don't have
to do anything inside it because it already extend
a message and all that, so you can just put pass.
And this too many pages read error
will essentially just be a copy of value error
but with a different name.
Note now that when we raise this error,
we're going to get a nice message there
that says, "Too many pages read error.
"You tried to read 85 pages,
"but this book only has 50 pages."
In addition, you get your traceback what tells you
where the problem was, which is that you tried
to read 50 pages there but you'd already read some before.
When you're looking back through your code,
you can very easily determine where abouts
in your code the problem happened
and you can read through it and try to find out
what the problem was.
Now of course, this isn't great for users,
so if you wanted to print a nicer message,
you can always try to do that.
And if not, except it, and print the message on its own.
Just like that.
That's it for creating custom classes.
It's really very straightforward.
You just inherit from the most
relevant built-in error class,
like value error, runtime error, or even type error
and you name it as you would like.
That gives you a slightly nicer name
and the ability to find your problems
a bit more easily.
Most of the time, you don't have to put anything
inside the error classes,
just giving them a new name is good enough.
But you can do, if you want.
We're not gonna cover how to populate the body
of your custom error classes in this course, though.
That's a little bit more advanced.
Thank you for joining me in this one
and I'll see you in the next video.