Python之异常处理

"""
异常处理
    包括异常捕获、抛异常和assert
    date:2023-01-29
"""

# 捕获异常
try:
    1 / 0
except ZeroDivisionError as e:
    print(e)  # division by zero

try:
    1 / 1
    520 + "hi"
except (ZeroDivisionError, TypeError) as e:
    print(e)
else:
    print("没有捕获异常!")
finally:
    print("进行收尾工作!")

# 抛异常
raise ValueError("值不正确!")

# assert,断言否则报错
assert 1 == 0, "值不相等!"

 

posted @ 2023-02-01 17:47  星瑞  阅读(27)  评论(0编辑  收藏  举报