User-defined exceptions
Python allows for a programmer to create new exceptions if the project warrants them. However, make sure that one of the built-in exceptions won't do the job for you. They have been tested and tweaked over the years and not only do they work effectively, but they have been optimized for performance and are bug-free.
Making your own exceptions involves object-oriented programming, which will be covered in Chapter 4, Functions and Object Oriented Programming, in the Classes, methods, and namespaces section. To make a custom exception, the programmer determines which base exception to use as the class to inherit from. For example, making an exception for negative numbers or one for imaginary numbers would probably fall ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access