0

How do I do the following in python? i.e. Get a status > 0 + message that I want to put out?

# code
exit(2, "there is an error in the given path") // Just a string example of course
khelwood
  • 55,782
  • 14
  • 81
  • 108
urie
  • 361
  • 2
  • 14
  • In python you would `try` something and if it doesn't work you `raise` the appropriate `Exception` with a custom message. Otherwise you can use `sys.exit("there is an error)`. – Guimoute Jan 30 '22 at 17:07

1 Answers1

0

if you want just throw an exception like what you did in your code, then you can use raise instead.

Difference between exit(0) and exit(1) in Python

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.