应该最终返回外部,并且异常处理是否完美? [英] should return outside finally and is the exception handled perfectly?
本文介绍了应该最终返回外部,并且异常处理是否完美?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是否应该将下面这个方法的返回结果放在最后? Pylint给出了错误的说法:
3: return statement in finally block may swallow exception (lost-exception)
Should I not put the return from this method below in finally ? Pylint gives error for this saying:
3: return statement in finally block may swallow exception (lost-exception)
def sendMessage(self, subject, msgContent, files, mailto):
""" Send the email message
Args:
subject(string): subject for the email
msgContent(string): email message Content
files(List): list of files to be attached
mailto(string): email address to be sent to
"""
msg = self.prepareMail(subject, msgContent, files, mailto)
# connect to server and send email
server=smtplib.SMTP(self.smtpserver, port=self.EMAIL_PORT)
server.ehlo()
# use encrypted SSL mode
server.starttls()
# to make starttls work
server.ehlo()
server.login(self.usrname, self.password)
server.set_debuglevel(self.debug)
try:
failed = server.sendmail(self.mailFrom, mailto, msg.as_string())
except Exception as er:
print er
finally:
server.quit()
if failed:
return False
return True
推荐答案
好,我解决了这个问题,@ Nabla指出了正确的答案!
alright, I fixed the problem, @Nabla pointed the right!!
def sendMessage(self, subject, msgContent, files, mailto):
""" Send the email message
Args:
subject(string): subject for the email
msgContent(string): email message Content
files(List): list of files to be attached
mailto(string): email address to be sent to
"""
msg = self.prepareMail(subject, msgContent, files, mailto)
# connect to server and send email
server = smtplib.SMTP(self.smtpserver, port=self.EMAIL_PORT)
server.ehlo()
# use encrypted SSL mode
server.starttls()
# to make starttls work
server.ehlo()
server.login(self.usrname, self.password)
server.set_debuglevel(self.debug)
try:
server.sendmail(self.mailFrom, mailto, msg.as_string())
except Exception as er:
print er
return False
finally:
server.quit()
return True
这篇关于应该最终返回外部,并且异常处理是否完美?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文