如何在python中引发多个异常
我尝试在if-else分支中引发多个异常: 代码如下:如何在python中引发多个异常,python,if-statement,exception,Python,If Statement,Exception,我尝试在if-else分支中引发多个异常: 代码如下: x = input() try: if int(x) < 0: # the number should not be a negative number raise("This is a negative number.") if int(x) >= 4: print("That a lot.") else: print("That is not so mu
x = input()
try:
if int(x) < 0: # the number should not be a negative number
raise("This is a negative number.")
if int(x) >= 4:
print("That a lot.")
else:
print("That is not so much.")
except ValueError: # the input should be a other than a number
print("You did not entered a number.")
x=input()
尝试:
如果int(x)<0:#该数字不应为负数
升起(“这是一个负数。”)
如果int(x)>=4:
打印(“太多了。”)
其他:
打印(“这并不多。”)
ValueError除外:#输入应为数字以外的数字
打印(“您没有输入数字。”)
出现以下错误消息:
TypeError:异常必须派生自BaseException创建自定义类,例如
class MyAwesomeException(AttributeError, KeyError):
"""
This exception occurs when an unknown language is requested.
"""
然后在代码中
raise MyAwesomeException(
'Language `{}` is not a valid language name or '
'not recognized by the program.'.format(item))
您不能键入raise(“这是一个负数”)
。相反,raise
需要一个有效的异常类,例如,raisevalueerror('这是一个负数')