From the course: Advanced Python
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Solution: Exceptions - Python Tutorial
From the course: Advanced Python
Solution: Exceptions
- [Instructor] Let's take a look at my solution. So we needed to implement a custom exception for our digital oven that was raised when an invalid value was given to the oven class. I chose to name my exception invalid temp error, and I derived this class from the base exception class, which is one of the best practices you should do. And of course, I provided a doc string to describe what the exception represents. The init function of the class accepts a temperature value, which is incorporated into the string that is displayed to the user in case the exception is raised. So here in my oven class, the code that sets the temperature checks to see if the value is zero, which turns the oven off. If it's not zero, then we check to see if it's less than 100 or greater than 500, which are invalid values and result in the exception being raised. So in my test code, I defined the exception handler to handle the invalid temp error and print out the result. And I also added an else block in…