Python – How to throw error and exit with a custom message in python

error handlingpython

I've seen people suggesting sys.exit() in Python.
My question is that, is there any other way to exit the execution of current script, I mean termination, with an error.

Something like this:

sys.exit("You can not have three process at the same time.")

Currently my solution would be:

print("You can not have three process at the same time.")
sys.exit()

Best Answer

Calling sys.exit with a string will work. The docs mention this use explicitly:

In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.