课程: Python Essential Training

Custom exceptions - Python教程

课程: Python Essential Training

Custom exceptions

- [Instructor] All right, this is an easy one. In fact, you already know how to do this, class CustomException extends Exception:pass. All right, we've written a custom exception. Next lesson. I'm just kidding. There are a few more things we need to cover. So here, we're using the pass statement because we literally don't need to define anything for our new CustomException class. It inherits the constructor of the Exception class that it's extending. The key information is in the name, CustomException. And often, this is all the information you need to know to help debug your app or let the user know what they're doing wrong. We can write a function that raises this new CustomException, def causeError:raise CustomException and then let's call this function. So we can see our CustomException error class in the name, then there's this colon, and then after that, it is curiously blank. So let's pass a custom message into our new class. You called the causeError function and you see now that message gets printed out. Custom exceptions are usually lightweight classes with very little in the way of special attributes and methods and things, but you might have some attributes that are useful for organizing and presenting information to the user about the error. For example, if you're writing a web server and need to raise HTTP exceptions at various points in the code, you might have an HttpException class and various specific HttpException classes that extend it. So let's write an HTTP exception with a static status code and a message attribute and then some information about how to format the string that it passes to the parent exception. So here's our HttpException. It's going to extend Exception. We're going to give it a status code. Let's make that None for now, so that's that special None value, and a message. We'll also make that None. And then we're going to override the parent constructor, so we need to define our own constructor, call the parent constructor. And then when we call that parent constructor, we're going to pass in the status code message, is statusCode and message is self.message, sorry, and that needs to be self.statusCode. Okay. Now let's write a couple child classes that extend the HttpException, NotFound HttpException. And all we need to do here is define our status code, which is going to be 404, and our message which is going to be Resource not found. Okay, now let's write a ServerError class, class ServerError extends HttpException statusCode 500 and message This server messed up! Great. Now let's write a function that say raises a ServerError, raiseServerError raise ServerError, and then let's call it. Great. And now you can see that this exception message gets formatted with our status code and message because it extends this HttpException. Writing custom exception classes is a great way to keep your code clean and organized. These classes act as documentation for all the problems that could happen, what cause them, what the solutions are, and they also separate common expected errors from something perhaps really bad that requires developer attention.

内容