点击查看代码
while True:
try:
x = int(input("请输入一个数字:"))
print("你输入的数字是:", x)
except:
print("异常,输入的不是数字:")
try:
a= input("请输入被除数:")
b=input("请输入除数:")
c=float(a)/float(b)
print(c)
except ZeroDivisionError:
print("异常:除数不能为零")
except TypeError:
print("异常:除数和被除数都应该为数值类型")
except BaseException as e:
print(e)
print(type(e))
class AgeError(Exception):
def __init__(self,errorInfo):
Exception.__init__(self)
self.errorinfo= errorInfo
def __str__(self):
return str(self.errorinfo)+",年龄错误!应该在1-150之间"
if __name__=="__main__":
age = int(input("输入一个年龄:"))
if (age < 1 or age > 150):
raise AgeError(age)
else:
print("正常的年龄:", age)