'Python can you have a try statement see if two separate functions will result in failure before executing either of them?

I'm writing a program that will get a users email, name, and age, and send them an email with their info and also save the info to an excel sheet saved on the users device. I am using openpyxl if that makes any difference

If the user is not connected to the internet, the program won't send an email, but will save it to the excel sheet. And if the user has the excel sheet open, but is connected to the internet, it will send an email, but not save it to the excel sheet.

I'm looking for a way to see if they will both work before executing either of them.

What I currently have are two separate try statements. The first one attempts to send the email, and gives an error if it fails. The second one attempts to save the excel sheet, and gives an error if it fails.

send_email() is a separate function that gets the users email and name, and sends them an email. wb is the workbook opened with openpyxl

    try:
        send_email(email, name)
    except:
        print(f'An error has occured. Please make sure you are connected to a valid network')
        
    try:
        wb.save(file)
    except:
        print('An error has occured. Make sure the file is closed')

I have tried putting one of the try statements into the other, but it still doesn't achieve the desired outcome

    try:
        send_email(email, name)
        try:
            wb.save(file)
        except:
            print('An error has occured. Make sure the file is closed')
    except:
        print(f'An error has occured. Please make sure you are connected to a valid network')

Doing it this way gets it to not save if the email doesn't work, but still sends the email if saving doesn't work.

Any help would be greatly appreciated, thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source