Interview Prep/Python for DevOps/Exceptions, context managers, files & logging

How do `sys.exit`, exit codes and uncaught exceptions relate?

BeginnerConceptExceptions, context managers, files & logging
Answer

sys.exit(n) raises SystemExit; the interpreter exits with n (0 success, non-zero failure). sys.exit("message") prints the message to stderr and exits 1. An uncaught exception prints a traceback and exits 1. A script that catches everything and falls off the end exits 0, which CI treats as success. Shell conventions: 2 for usage errors (argparse and click use it), 126/127 for not executable/not found, 128+N for killed by signal N.

All Python for DevOps questions Roadmap: Python for automation

More on Exceptions, context managers, files & logging