From the course: Advanced Python
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Custom exceptions - Python Tutorial
From the course: Advanced Python
Custom exceptions
- [Instructor] While Python provides a range of built-in exceptions, there are cases where you may want to create custom exceptions to handle specific error conditions in your code. In this example, I'll demonstrate how to create and use custom exceptions in Python. Let's go ahead and open up the custom_exceptions example in the Start folder. So, let's consider a simple bank account program that keeps track of withdrawals and deposits. So deposits increase the balance and withdrawals will decrease the balance. Of course, you shouldn't be able to take out more money than the account has in it. So let's modify the program here to raise a custom exception in this case. So first, let's create our custom exception and we'll call the InsufficientFundsError. So I'll define a new class called InsufficientFundsError, and that is going to derive from the base Exception class. And we'll just put in a quick doc string that says it's raised when the account balance is insufficient for a…