如何在 Python 中处理异常?

pythonserver side programmingprogramming更新于 2025/6/25 15:08:17

在 Python 中处理异常的最简单方法是使用"try-except"块。

示例

try:
fob = open("test.txt", "r")
fob.write("This is my test file for exception handling!!")
except IOError:
print "Error: can\'t find the file or read data"
else:
print "Write operation is performed successfully on the file"
fob.close()

输出

Error: can't find the file or read data

相关文章


有用资源