Types of Exceptions
Lesson 10 of 16 in Coddy's Exception Handling in Python course.
There are two types of Exceptions:
Built-in Exceptions:
- These Exceptions are already available in python.
- ex. EOFError, IndexError, TypeError, ValueError etc
User-defined Exceptions (Custom Exceptions):
- These are created by programmers.
- ex. Raising Exception if user enters invalid password.
Remember:
- Every Exception is actually a class in Python inherited from 'BaseException' built in class.
- So, IndexError is also built-in class inherited from BaseException class.
- As soon as exception occurs, an object is created for raised Exception.
Challenge
EasyClick on RUN CODE and you will see the IndexError Inheritance hierarchy.
Try it yourself
#Click on RUN CODE and observe
def observe(idx):
try:
data = [10,20,30,40]
print(data[idx]) #IndexError
except Exception as e:
print(type(e)) #Shows Exception is class
print(IndexError.mro()) #Shows all parent classesAll lessons in Exception Handling in Python
4Customization/best practices
Customize Tracebacks3User-defined Exceptions
Types of ExceptionsRaising an ExceptionUser Defined ExceptionException Handling Exercise- 1Exception Handling Example-2Exception Handling Example-3