Home > Software design >  Python - Manage errors message (email sending)
Python - Manage errors message (email sending)

Time:06-24

I'm trying to print a message when there's no error during the shipping of the email and doing an action if there's an error.

I gave it a try but nothing worked, the error message stop everything.

How hide the error message and replace it with my own and keep with an action.

 import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = '[email protected]'
mail.Subject = 'TEST'
mail.Body = 'Hello'


mail.Send()

try :
    print("mail sent")

except BaseException as e:
    print("mail not sent")
    fonction()

CodePudding user response:

You need write the code you want to test inside try block. And for your message printing problem you can try the else block of try-except. else block executes if there is no error in try block. So try this:

import win32com.client as win32

try :
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = '[email protected]'
    mail.Subject = 'TEST'
    mail.Body = 'Hello'
    mail.Send()

except BaseException as e:
    print("mail not sent")
    fonction()
else:
    # this part is executed if there is no error
    print("mail sent")
  • Related