Chaining Multiple Excepts #
We can handle multiple error types differently by using multiple excepts.
Here's an example on handling ZeroDivisionError and TypeError differently:
try:
# code to run
except ZeroDivisionError:
# code to run when ZeriDivisionError is raised
except TypeError:
# code to run when TypeError is raised
Exercise #
- Update the function
divide_the_numberto handle multiple types of error.
Tests #
divide_the_number(4, 2)should return2.0divide_the_number(10, 4)should return2.5divide_the_number(10, 0)should print the message"You cannot divide by zero!"divide_the_number(10, "Hello")should print the message"You cannot divide a number with a string!"
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