尝试/除外检查字符串

用户名

我对如何使用Try/Exception感到困惑if/else如果要告诉用户提供.html文件,我该如何编写惯用代码

if url[-4:] ==".html":
        // do your things
else: 
    print('Error! the file is not html file')

我正在检查是否应该try/exception在这种情况下使用if/else像以前那样使用。

乔纳森·戴维斯(Jonathan Davies)

简而言之:

try:
    a = q.get()

try表示尝试使用该工具,如果可以使用,请使用它,否则,请尝试except其他方法,如果它失败或可以使用,并且出现诸如的错误ValueError

except:
    a = None

更新:

try:
   url[-4:] == ".html"

except: 
    print "Error"

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章