抛出自定义异常:
- 如:当用户输入内容不是整数时,提示”请输入整数 ”
- 使用
raise Exception('必须输入整数')
def number():
score = input('请输入数字:')
if score.isdigit():
print(score)
else:
raise Exception('必须输入整数')
try:
number()
except Exception as err:
print(err)
else:
print('输入数字成功')
---
请输入数字:1.1
必须输入整数
---