Handling multiple errors with a single except #
Sometimes, we might prefer to handle multiple error types in a single except statement. We can do so by providing multiple error types in parentheses, separated by comma , after except.
For example:
try:
# Code to run
except (ZeroDivisionError, TypeError):
# Code to run when ZeroDivisionError or TypeError was raised
Exercise #
- Update the function
divide_the_numberto handle multiple error types
Tests #
divide_the_number(4, 2)should return2.0divide_the_number(10, 4)should return2.5divide_the_number(10, 0)should print the message"Something is wrong!"divide_the_number(10, "Hello")should print the message"Something is wrong!"- Must use only one
exceptinside the function
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules