简体   繁体   中英

Python exceptions - catching all exceptions but the one expected

I am working on a simple automation script in Python, which could throw exceptions in various spots. In each of them I would like to log a specific message and exit the program. In order to do that, I raise SystemExit after catching the exception and handling it (performing specific logging operations and such).

In the top-level calling of main, I do the following:

if __name__ == "__main__":
    try:
        main()
    except SystemExit:  # handled exception
        sys.exit(1)
    except:  # any unhandled exception
        logging.error('Unexpected error: ', exc_info=True)
        sys.exit(2)

However, using a bare except is something frowned upon. Is using an "exception tree" where I use a bare except to specify "anything but the exception that I've handled" a nonstandard way? Is there a better way to achieve this? I would still like to log these unhandled exceptions, even if they were not handled.

Edit: SystemExit is raised to note that an exception has been handled - no matter what the exception is in my case, I always want to stop running the scripts as any failure should result in an absolute failure.

The main reason I'm asking this is that PEP8 seems to consider using a bare except as an error, and even though I could use except BaseException, it should be just a syntactic difference. Is one way more standard than the other or is there another standard route of achieving this?

Bare exceptions trap things you do not want to trap, such as GeneratorExit . Do it this way:

except Exception as details:
    logging.error('Unexpected error: {0}'.format(details))

The main issue with a bare except is that it can catch things like SystemExit and KeyboardInterrupt which are not standard 'code' errors and shouldn't usually be handled in the same way as an exception generated by your code. Using the Exception class doesn't cover those cases as they do not inherit from it, so it is more than a syntax difference.

https://docs.python.org/2/howto/doanddont.html#except https://docs.python.org/3.1/howto/doanddont.html#except

If you want to handle those specific cases, then it is better to do so explicitly as you have done for SystemExit.

This worked for me:

try:
   <code>
   raise Exception("my error")
except Exception as e:
    raise e

If my error occurs then the error message "my errror" is seen. If an unknown exception occurs then it displays the default exception handler's text. In either case an exception is raised and the script is halted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM