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

后端 未结 4 546
时光取名叫无心
时光取名叫无心 2021-02-03 16:55

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.

<
4条回答
  •  再見小時候
    2021-02-03 17:19

    I know this is an old thread, however you can also raise an error like this:

    raise SystemExit('Error: 3 processes cannot run simultaneously.')

    One advantage of this approach is that you don't have to import the Python sys module. This works on Linux with Python 3 and Python 2. I have not tested it on Windows or Mac OS.

提交回复
热议问题