Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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") ?

like image 314
elbajo Avatar asked Sep 27 '13 09:09

elbajo


1 Answers

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.

like image 109
Martijn Pieters Avatar answered Nov 15 '22 08:11

Martijn Pieters