求指导:"异常处理try_except语句代码中的隐藏bug"
小白在此求助给为路过的大佬帮忙审查一下bug出在哪里,感激不尽!工作环境:win10系统, Python3.6.5, idlex编程环境.
代码如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
#针对不同异常设置多个except:
try:
sum1 = 1 + "1"
f = open("我是一个不存在的文档呀.txt")
print(f.read())
f.close()
except TypeError as reason:
print("类型出错啦T_T\n错误原因是:", +str(reason))
except OSError as reason:
print("文件出错啦T_T\n错误原因是:", +str(reason))
----------------------------------------------------------------运行结果出错:
Traceback (most recent call last):
File "D:/Python源代码组/fish_05.25.py", line 31, in <module>
sum1 = 1 + "1"
TypeError: unsupported operand type(s) for +: 'int' and 'str'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/Python源代码组/fish_05.25.py", line 36, in <module>
print("类型出错啦T_T\n错误原因是:", +str(reason))
TypeError: bad operand type for unary +: 'str'
==========================================
代码来源于小甲鱼<零基础入门Python>第二版,page121,照葫芦画瓢也能出bug,我今晚真想喝碗甲鱼汤补补脑了,哼!
print("文件出错啦T_T\n错误原因是:", +str(reason))
改为
print("文件出错啦T_T\n错误原因是:" +str(reason))
去掉逗号 南归 发表于 2021-5-25 14:57
print("文件出错啦T_T\n错误原因是:", +str(reason))
改为
print("文件出错啦T_T\n错误原因是:" +str(r ...
感谢南归小姐姐,眼光犀利,一眼看出我自己给自己挖的坑{:5_105:}.今晚要好好洗一下眼睛{:7_125:} 憨豆逗我 发表于 2021-5-25 15:07
感谢南归小姐姐,眼光犀利,一眼看出我自己给自己挖的坑.今晚要好好洗一下眼睛
emmm,可惜我不是小姐姐 本帖最后由 憨豆逗我 于 2021-5-26 13:40 编辑
南归 发表于 2021-5-25 15:34
emmm,可惜我不是小姐姐
好尴尬啊,哈哈哈!总之谢谢大佬们
页:
[1]