I could see several topics on try - catch but doesnt seem to discuss errors if any from finally block itself. I found that the error is not handled if it is in finally block. What would be the ideal way to manage finally?
For eg. below is a mail function. if there is any error in try block, finally will execute the quit method which itself is not initiated so an unhandled error occurs. So is it better to ensure there is no errors occur in finally block?
def send_email(ldap, email_address, password, msg): try: message = MIMEMultipart('alternative') message['To'] = email.utils.formataddr(('Recipient', '%[email protected]'%email_address)) message['From'] = email.utils.formataddr(('Author', '%[email protected]'%email_address)) message['Subject'] = 'Sample subject' text = "%s"%msg html = MIMEText('<html><head></head><h2>data</h2><body><p>%s</p></body></html>'%msg,'html') message.attach(html) server = smtplib.SMTP(host="ip",port=0) server.set_debuglevel(True) # identify ourselves, prompting server for supported features server.ehlo() if server.has_extn('STARTTLS'): server.starttls() server.ehlo() server.login(ldap, password) print "%[email protected], %[email protected], %s "%(email_address,email_address,message.as_string()) server.sendmail('%[email protected]'%email_address, "%[email protected]"%email_address, message.as_string()) finally: server.quit()
try?