Python 异常

1.处理ZeroDivisionError异常

【实例】:

try:
    print(5/0)
except ZeroDivisionError:
    print("division by zero")

【运行结果】:

division by zero

 

2.处理FileNotFoundError异常

【实例】:

file = 'da.txt'
try:
    with open(file,encoding='utf-8') as f:
        contents = f.read()
except FileNotFoundError:
    print("File not found")

【运行结果】:

File not found

 

φ(゜▽゜*)♪ 感谢观看,希望对你有帮助!

posted @ 2022-10-26 10:02  Mr_宋先生  阅读(16)  评论(0编辑  收藏  举报