Python编程从入门到实践~异常
生活随笔
收集整理的這篇文章主要介紹了
Python编程从入门到实践~异常
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
#異常
try:print(5/0)
except ZeroDivisionError:print("You can't divide by zero!")#else 代碼塊
try:answer = print(5/0.99)
except ZeroDivisionError:print("You can't divide by zero!")
else:print(answer)#處理FileNotFoundError
filename = 'alice.txt'
try:with open(filename) as file_object:contents = file_object.read()
except FileNotFoundError:print(f"Sorry, the file {filename} does not exist.")#靜默失敗
filename = 'alice.txt'
try:with open(filename) as file_object:contents = file_object.read()
except FileNotFoundError:pass
else:print(contents)
總結(jié)
以上是生活随笔為你收集整理的Python编程从入门到实践~异常的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java加密与解密的艺术~RSA模型分析
- 下一篇: Python编程从入门到实践~函数