Python 官方文档:入门教程 => 点击学习
代码有问题时,python会显示traceback,后面跟着一条错误信息。这个错误被称为异常(exception)。 格式try: 可能产生运行时错误的代码except: 错误恢复代码测试文本Man: Is this your
try:
可能产生运行时错误的代码
except:
错误恢复代码
Man: Is this your car?
Other man: No.
Woman: What color is it?
Bye.
Go on
#!/usr/local/Python
with open('E:/python code/other/tmp/test.txt') as fd:
for each_line in fd:
if each_line.find(':') > 0:
(role, line_spoken) = each_line.split(':',1)
print(role),
print(' said '),
print(line_spoken),
#!/usr/local/python
import os,sys
try:
with open('E:/python code/other/tmp/test.txt') as fd:
for each_line in fd:
try:
(role, line_spoken) = each_line.split(':', 1)
print(role),
print(' said '),
print(line_spoken),
except ValueError:
pass
except IOError:
print('The file is missing.')
--结束END--
本文标题: 第3章 处理异常
本文链接: https://www.lsjlt.com/news/184936.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0