gpt4 book ai didi

python - 应该最终返回外面并且异常处理是否完美?

转载 作者:太空宇宙 更新时间:2023-11-04 03:49:54 28 4
gpt4 key购买 nike

我不应该把这个方法的返回值放在下面的 finally 中吗? Pylint 给出了这句话的错误:3: finally block 中的 return 语句可能会吞下异常 (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 指向了右边!!

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

关于python - 应该最终返回外面并且异常处理是否完美?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21808107/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com