Editor settings

General settings

Save settings

Learn to program with Python

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_number to handle multiple types of error.

Tests #

  • divide_the_number(4, 2) should return 2.0
  • divide_the_number(10, 4) should return 2.5
  • divide_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!"
Output will be displayed here