# --------------------------------- 第一题(课中) ----------------------------------#
class AgeException(RuntimeError):
def __init__(self,age):
super().__init__()
self.age = age
def enterage(age):
if age < 0:
raise AgeException("Only positive integers are allowed")
if age % 2 == 0:
print("age is even")
else:
print("age is odd")
try:
num = int(input("Enter your age:"))
enterage(num)
except AgeException:
print("Only integers are allowed")
except:
print("something is wrong")
# --------------------------------- 第二题(课中) ----------------------------------#
# 重新审视以前练习中的程序,检查哪些程序需要使用异常处理,请进行优化。
python异常处理(参考答案)
最新推荐文章于 2023-03-21 19:43:07 发布