Python中没有报错提示的代码示例-创新互联

创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!

平鲁网站建设公司创新互联,平鲁网站设计制作,有大型网站制作公司丰富经验。已为平鲁数千家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的平鲁做网站的公司定做!

小编给大家分享一下Python中没有报错提示的代码示例,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨方法吧!

Python中没有报错提示的代码示例:

def count_words(filename):
    try:
        with open(filename) as f_obj:
            contents=f_obj.read()
    except FileNotFoundError:
        pass    
    else:
        words=contents.spilt()
        num_words=len(words)
        print("This file"+filename+'has about'+str(num_words)+'words.')
===========================================================================================
def count_words(filename):
    try:
        with open(filename) as f_obj:
            contents=f_obj.read()
    except FileNotFoundError:
        message='sorry,the file'+filename+'does not exits'
        print(message)
    else:
        words=contents.spilt()
        num_words=len(words)
        print("This file"+filename+'has about'+str(num_words)+'words.')

原因:

其中使用了 try-except-else 的代码块,except是try代码中错误时执行,而except中的代码是pass,也就是在try中代码错误时候直接pass,不给用户编写任何的建议和help。

相应报错的代码也写在了横线下面。

看完了这篇文章,相信你对Python中没有报错提示的代码示例有了一定的了解,想了解更多相关知识,欢迎关注创新互联-成都网站建设公司行业资讯频道,感谢各位的阅读!


本文题目:Python中没有报错提示的代码示例-创新互联
浏览路径:http://myzitong.com/article/dsehcs.html