Is there any difference between exit() and raise ValueError("example") except for the fact that I will have an error print on my output when using raise ValueError("example") ?
1 Answer
There is a huge difference.
sys.exit() raises a SystemExit exception, which Python always catches and turns into a program exit code.
Raising ValueError, if uncaught, triggers the sys.excepthook() handler, after which Python exits. The default except hook prints the traceback of the exception to stderr, after which Python exits with an exit code of 1.
The sys.excepthook() function is never called for SystemExit, so you cannot customize the handling of that exception, but you can handle the handling of ValueError and other exceptions.
exitexits, whileraiseraises an error, which indicates there has been an error, and can be caughtsys.exit()raisesSystemExit, which terminates the interpreter without a traceback, but the principle is the same, yes.