I'm trying to figure out if there's a more pythonic way to do a specific error handling task. I want to catch multiple specific custom exceptions and do something specific depending on which exception is caught (like use a specific custom exit code, or add a specific log message, etc...). However, I also want, if any exception is raised, to do send an email saying the script didn't complete successfully, had exit code __, the log is at C:\foo\bar.txt
I know I could include everything I want to do in each except, such as:
try:
do_something()
except CustomError1:
exitcode = 1
send_error_email(exitcode)
sys.exit(exitcode)
except CustomError2:
exitcode = 2
send_error_email(exitcode)
sys.exit(exitcode)
But I'm wondering if there's a more pythonic or more efficient way of doing it. I'm imagining something like
try:
do_something()
except CustomError1:
exitcode = 1
except CustomError2:
exitcode = 2
except ParrentCustomErrorClass:
send_error_email()
sys.exit(exitcode)
If it matters, I'm currently stuck with python 2.7, but need to be able to port solution to 3.x when 3rd party applications allow it.