Paul's Python CoursePPC

Exit codes

Not met yet

Every program finishes with an exit code, a number for whoever started it: 0 means it went well, anything else means it didn't. sys.exit(1) ends the whole program on the spot with code 1, from wherever it's called. Reaching the end, or sys.exit() with no number, gives 0. Scripts read the code to decide what to do next, so a failure that exits with 0 is a quiet lie.

Try it

main.py

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks sys.exit() only leaves the current function, like return, and the program carries on.
  • Thinks exit code 0 means something went wrong, the way 0 is falsy.
  • Thinks the exit code doesn't matter as long as an error message is printed.

Where it comes up