Аргумент, переданный исключению в операторе "исключение" python, вызывает TypeError - PullRequest
0 голосов
/ 28 апреля 2020

Я попытался запустить следующий код

try:
    number = input("Enter a number ")
    print(5/int(number))
except ZeroDivisionError("No Zero") as e1:
    print('The number cannot be zero ',e1)
except ValueError as e2:
    print("The entered character is not a number ",e2)
except Exception as e: # This is a generic exception statement
    print("Some unknown exception occurred ",e)
finally: # Always executes
    print("This statement will always execute irrespective of exception is raised or not")

Возникла следующая ошибка

Enter a number 0
This statement will always execute irrespective of exception is raised or not
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-17-32893f7d08e1> in <module>
      3     number = input("Enter a number ")
----> 4     print(5/int(number))
      5 except ZeroDivisionError("No Zero") as e1:

ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-17-32893f7d08e1> in <module>
      3     number = input("Enter a number ")
      4     print(5/int(number))
----> 5 except ZeroDivisionError("No Zero") as e1:
      6     print('The number cannot be zero ',e1)
      7 except ValueError as e2:

TypeError: catching classes that do not inherit from BaseException is not allowed

Я не могу понять, почему при добавлении аргумента к ошибке возникает ошибка TypeError

...