python sys.exit not working in try

53,800

Solution 1

sys.exit() raises an exception, namely SystemExit. That's why you land in the except-block.

See this example:

import sys

try:
    sys.exit()
except:
    print(sys.exc_info()[0])

This gives you:

<type 'exceptions.SystemExit'>

Although I can't imagine that one has any practical reason to do so, you can use this construct:

import sys

try:
    sys.exit() # this always raises SystemExit
except SystemExit:
    print("sys.exit() worked as expected")
except:
    print("Something went horribly wrong") # some other exception got raised

Solution 2

based on python wiki :

Since exit() ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.

And:

The exit function is not called when the program is killed by a signal, when a Python fatal internal error is detected, or when os._exit() is called.

Therefore, If you use sys.exit() within a try block python after raising the SystemExit exception python refuses of completing the exits's functionality and executes the exception block.

Now, from a programming perspective you basically don't need to put something that you know definitely raises an exception in a try block. Instead you can either raise a SystemExit exception manually or as a more Pythonic approach if you don't want to loose the respective functionalities of sys.exit() like passing optional argument to its constructor you can call sys.exit() in a finally, else or even except block.

Method 1 (not recommended)

try:
    # do stuff
except some_particular_exception:
    # handle this exception and then if you want 
    # do raise SystemExit
else:
    # do stuff and/or do raise SystemExit
finally:
    # do stuff and/or do raise SystemExit

Method 2 (Recommended):

try:
    # do stuff
except some_particular_exception:
    # handle this exception and then if you want 
    # do sys.exit(stat_code)
else:
    # do stuff and/or do sys.exit(stat_code)
finally:
    # do stuff and/or do sys.exit(stat_code)
Share:
53,800

Related videos on Youtube

onlyvinish
Author by

onlyvinish

Python enthusiastic

Updated on July 09, 2022

Comments

  • onlyvinish
    onlyvinish almost 2 years
    Python 2.7.5 (default, Feb 26 2014, 13:43:17)
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> try:
    ...  sys.exit()
    ... except:
    ...  print "in except"
    ...
    in except
    >>> try:
    ...  sys.exit(0)
    ... except:
    ...  print "in except"
    ...
    in except
    >>> try:
    ...  sys.exit(1)
    ... except:
    ...  print "in except"
    ...
    in except
    

    Why am not able to trigger sys.exit() in try, any suggestions...!!!

    The code posted here has all the version details.

    I have tried all possible ways i know to trigger it, but i failed. It gets to 'except' block.

    Thanks in advance..

    • Martijn Pieters
      Martijn Pieters over 9 years
      I closed this as a duplicate, because the other post explains a) what goes wrong and b) how to avoid this. Blanket except: clauses are not a good idea, at best use except Exception: to avoid catching SystemExit.
    • Back2Basics
      Back2Basics over 9 years
      use os._exit(0) if you don't want to trigger an exception.
    • Martijn Pieters
      Martijn Pieters over 9 years
      @Back2Basics: Don't call os._exit(); that just ensures nothing gets cleaned up as the processor exits.
  • onlyvinish
    onlyvinish over 9 years
    Is there any way to do this in try block.
  • tamasgal
    tamasgal over 9 years
    There is no reason, since what you try to achieve is raising an exception in try and "catch" it in the next moment. It is absolutely clueless ;-)
  • Vincent
    Vincent over 9 years
    Use except Exception, because SystemExit inherits directly from BaseException. See the exception hierarchy
  • onlyvinish
    onlyvinish over 9 years
    Thanks buddy..!! If u come across any idea regarding this, plz do update me.
  • tamasgal
    tamasgal over 9 years
    See the updated code example to achieve what you wanted. However, still no idea why you would do that.
  • Martijn Pieters
    Martijn Pieters over 9 years
    Don't call os._exit() yourself, just don't catch SystemExit. Calling os._exit() means nothing gets cleaned up either.
  • glglgl
    glglgl over 9 years
    @onlyvinish He already gave you an idea! What else are you missing?