Python
exit codes
exit(0)
exit(1)
programming errors

Difference between exit0 and exit1 in Python

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

sys.exit(0) and sys.exit(1) both stop a Python program, but they send different messages to the operating system and anything that launched the script. 0 means success. 1 means failure.

That difference matters most when the script is part of automation. A shell script, CI job, cron task, or parent process often ignores printed output and looks only at the exit code.

Use sys.exit() in real scripts

In actual programs, use sys.exit() rather than the interactive helper exit():

python
import sys

sys.exit(0)

exit() and quit() are mainly conveniences for the interactive interpreter. sys.exit() is the normal script-level API.

What 0 means

An exit code of 0 conventionally means the program completed successfully.

python
1import sys
2
3def main():
4    print("Backup completed")
5    sys.exit(0)
6
7main()

To a shell or automation system, that is the signal that the step succeeded.

What 1 means

A nonzero exit code signals failure. 1 is the most common generic error code.

python
1import sys
2
3def main():
4    print("Configuration file missing")
5    sys.exit(1)
6
7main()

The exact meaning of 1 is up to the program, but the shared convention is simple: zero is success, nonzero is error.

Why automation cares

Consider a shell script:

bash
python deploy_check.py
echo $?

If the Python script exits with 0, the shell prints 0. If it exits with 1, the shell prints 1, and follow-up logic may stop or switch into an error path.

That is why printing an error message is not enough. If the script still exits with 0, automation will treat the run as successful.

sys.exit() raises SystemExit

Inside Python, sys.exit() works by raising the SystemExit exception.

python
1import sys
2
3try:
4    sys.exit(1)
5except SystemExit as exc:
6    print("Caught exit code:", exc.code)

If SystemExit is not caught, Python terminates and returns the code to the operating system. If it is caught, the process may keep running.

This is useful in tests, but it also explains why a call to sys.exit() can sometimes appear to "not work" when a framework intercepts the exception.

A clean pattern for command-line tools

A common structure is to let main() return a numeric status and pass it into sys.exit():

python
1import sys
2
3def main():
4    ok = False
5    if not ok:
6        return 1
7    return 0
8
9if __name__ == "__main__":
10    sys.exit(main())

This keeps the control flow simple and makes success or failure explicit at the end of the program.

Use nonzero codes intentionally

Many small scripts use only 0 and 1, and that is perfectly fine. Larger tools often assign different nonzero codes to different failure categories so calling scripts can react more precisely.

For example, 2 might mean bad arguments and 3 might mean missing configuration. The central rule stays the same: success is zero, and any nonzero status tells automation that something went wrong.

Common Pitfalls

The most common mistake is assuming exit(0) and exit(1) are interchangeable because both stop the script. They are not interchangeable to the caller.

Another issue is printing an error message and then returning success with 0. That makes logs look bad while automation still reports a pass.

Developers also sometimes use exit() in scripts because it works locally. sys.exit() is the clearer and more standard choice.

Finally, remember that SystemExit can be caught. If some outer layer intercepts it, the process may continue unless the exception is re-raised.

Summary

  • 'sys.exit(0) means successful completion.'
  • 'sys.exit(1) means a generic failure.'
  • Shells and CI systems rely on that distinction.
  • 'sys.exit() works by raising SystemExit.'
  • Use sys.exit() rather than exit() in real Python scripts.

Course illustration
Course illustration

All Rights Reserved.