How do I handle both specific and generic exceptions?

78 views

How do I handle both specific and generic exceptions?

How do I handle both specific and generic exceptions?

solveurit24@gmail.com Changed status to publish February 13, 2025
0

Use multiple except blocks or a generic except Exception.

try:
    # Code that might throw an error
    open("non_existent_file.txt", 'r')
except FileNotFoundError:
    print("The file was not found.")
except Exception as e:
    print("An error occurred:", e)

solveurit24@gmail.com Changed status to publish February 13, 2025
0