Skip to content
Advertisement

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

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()

Advertisement

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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement