exit(0) means a clean exit without any errors / problems. exit(1) means there was some issue / error / problem and that is why the program is exiting. This is not Python specific and is pretty common. A non-zero exit code is treated as an abnormal exit, and at times, the error code indicates what the problem was.
In C++, what is the difference between exit(0) and return 0 ? When exit(0) is used to exit from program, destructors for locally scoped non-static objects are not called. But destructors are called if return 0 is used.
Exit Code 1 indicates that a container shut down, either because of an application failure or because the image pointed to an invalid file. In a Unix/Linux operating system, when an application terminates with Exit Code 1, the operating system ends the process using Signal 7, known as SIGHUP.
0 and 1 are the exit codes.
exit(0)
means a clean exit without any errors / problems
exit(1)
means there was some issue / error / problem and that is why the program is exiting.
This is not Python specific and is pretty common. A non-zero exit code is treated as an abnormal exit, and at times, the error code indicates what the problem was. A zero error code means a successful exit.
This is useful for other programs, shell, caller etc. to know what happened with your program and proceed accordingly.
This determines the exit status of the program when it finishes running (generally, 0 for success and 1 for error).
It is not unique to Python, and the exact effect depends on your operating system and how the program is called (though 99% of the time, if you're just running Python scripts, it doesn't matter).
The standard convention for all C programs, including Python, is for exit(0)
to indicate success, and exit(1)
or any other non-zero value (in the range 1..255) to indicate failure. Any value outside the range 0..255 is treated modulo 256 (the exit status is stored in an 8-bit value). Sometimes, that will be treated as signed (so you might see -128, -127, etc) but more usually it is treated as unsigned.
This status is available to the code that invoked Python. This convention applies across platforms, though the meaning of non-zero exit status can vary on different platforms.
The number you pass to the exit()
function is simply your program's return code, which is given to the operating system. From your program's point of view, there is no difference: execution will end in both cases, and the value supplied to the function will be given to the OS. But some tools and scripts take into account the program's exit code. Most tools return 0 when they succeed and nonzero to indicate an error.
So, if your program will be run from a script, an automated tool or from some other software that takes into account the return code (such as an IDE), you must be careful on what you return.
When in doubt, just return 0 to indicate everything is OK.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With