Editor settings

General settings

Save settings

Learn to program with Python

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

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 "Something is wrong!"
  • divide_the_number(10, "Hello") should print the message "Something is wrong!"
  • Must use only one except inside the function
Output will be displayed here