Alex
Alex

Reputation: 1994

Why my python code terminates even though I have proper exception handling?

I have the following python code:

try:
    subprocess.check_output('service uwsgi stop;pkill -f uwsgi', shell = True)
except:
    sys.exit(0)

it should return always 0 but when I run it, it prints 'Terminated' and then I receive a non-zero return code.

Upvotes: 0

Views: 44

Answers (1)

erickque
erickque

Reputation: 126

Right now, sys.exit(0) will only be called when an exception is raised. To ensure that it is called every time, add a finally statement:

try:
    subprocess.check_output('service uwsgi stop;pkill -f uwsgi', shell = True)
except:
    # Handle the exception
finally:
    sys.exit(0)

Upvotes: 2

Related Questions