5

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

2
  • exit exits, while raise raises an error, which indicates there has been an error, and can be caught Commented Sep 27, 2013 at 9:20
  • 2
    sys.exit() raises SystemExit, which terminates the interpreter without a traceback, but the principle is the same, yes. Commented Sep 27, 2013 at 9:20

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.