![图片[1]-python没有报错提示-uusu优素-乐高,模型,3d打印,编程](http://uusucn.zbbe.cn/wp-content/uploads/2024/01/5ee9dec58fafa376.jpg)
Python中没有报错提示的代码示例:
defcount_words(filename):
try:
withopen(filename)asf_obj:
contents=f_obj.read()
exceptFileNotFoundError:
pass
else:
words=contents.spilt()
num_words=len(words)
print("Thisfile"+filename+'hasabout'+str(num_words)+'words.')
===========================================================================================
defcount_words(filename):
try:
withopen(filename)asf_obj:
contents=f_obj.read()
exceptFileNotFoundError:
message='sorry,thefile'+filename+'doesnotexits'
print(message)
else:
words=contents.spilt()
num_words=len(words)
print("Thisfile"+filename+'hasabout'+str(num_words)+'words.')
原因:
其中使用了 try-except-else 的代码块,except是try代码中错误时执行,而except中的代码是pass,也就是在try中代码错误时候直接pass,不给用户编写任何的建议和help。
相应报错的代码也写在了横线下面。
原文来自:https://www.py.cn© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END

















































暂无评论内容