我对如何使用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
像以前那样使用。
简而言之:
try:
a = q.get()
try
表示尝试使用该工具,如果可以使用,请使用它,否则,请尝试except
其他方法,如果它失败或可以使用,并且出现诸如的错误ValueError
。
except:
a = None
更新:
try:
url[-4:] == ".html"
except:
print "Error"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句