/ / Python 3 помилка кидання для обробки винятків - python, python-3.x, обробка винятків

Помилка викидання Python 3 для обробки виключень - python, python-3.x, обробка винятків

Я почав вивчати пітон на минулому тижні, і я не можу зрозуміти, що тут не так:

def add(x,y):
"""Adds 2 numbers and returns the result"""
return x+y

def sub(x,y):
"""Subtracts 2 numbers and returns the result"""
return x-y


a = int(input("Enter first number"))
b = int(input("Enter second number"))
c = int(input("Enter 1 for subtraction , 2 for addition and 3 for both"))
try:
if c>3:
raise ValueError()
except ValueError():
print ("Wrong choice")
else:
print ("Your choice is ",c)
if (c==1):
print ("Subtraction of 2 numbers=",(sub(a,b)))
if (c==2):
print ("Addition of 2 numbers = ",(add(a,b)))
if (c==3):
print ("Subtraction of 2 numbers=",(sub(a,b)))
print ("Addition of 2 numbers = ",(add(a,b)))

Якщо я введу 4, вона видає цю помилку:

Traceback (most recent call last):
File "C:/Program Files (x86)/Python35-32/calculator.py", line 15, in <module>
raise ValueError()
ValueError

Під час обробки вищезгаданого винятку відбулося ще одне виключення:

Traceback (most recent call last):
File "C:/Program Files (x86)/Python35-32/calculator.py", line 16, in <module>
except ValueError():
TypeError: catching classes that do not inherit from BaseException is not allowed

Відповіді:

4 для відповіді № 1

Ви намагаєтеся зловити екземпляр від ValueError(), де Python очікує, що ви фільтруєте на тип. Видалити виклик:

except ValueError:
print ("Wrong choice")