Python 2.7: difference between exit () and raising ValueError ("example")

Is there any difference between exit()and raise ValueError("example"), with the exception of the fact that I will have a print error on my output when using raise ValueError("example")?

+4
source share
1 answer

There is a huge difference.

sys.exit()throws an exception SystemExitthat Python always catches and turns into program exit code.

Raising ValueError, if not displayed, starts the handler sys.excepthook(), after which Python exits. The default value, except for hook, prints the exception trace before stderr, after which Python completes the exit code from 1.

sys.excepthook() SystemExit, , ValueError .

+6

All Articles