温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-06-28 11:12:57 来源:亿速云 阅读:273 作者:清晨 栏目:编程语言

小编给大家分享一下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中没有报错提示的代码示例有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI