Python provides a variety of
built-in exceptions that you can use to handle errors that often occur in programs.
These built-in exceptions make it easier for developers to catch common mistakes and handle them elegantly. Some common built-in exceptions are:
- ZeroDivisionError: Raised when a division by zero occurs.
- IndexError: Raised when trying to access an index that is not in a list.
- KeyError: Raised when a dictionary key is not found.
- TypeError: Raised when an operation is performed on an inappropriate type.
Using these built-in exceptions, you can enhance the robustness of your code by providing graceful error handling mechanisms. For example, calling an index that does not exist will automatically raise an
IndexError:
``` python
my_list = [10, 20, 30]
print(my_list[5]) # This will raise an IndexError
```