I have a small python script which I have written specifically for this question.
#!/usr/bin/python3
import sys
def testfunc(test):
if test == 1:
print("test is 1")
else:
print("test is not 1")
sys.exit(0)
try:
testfunc(2)
except:
print("something went wrong")
print("if test is not 1 it should not print this")
What I was expecting is that when test = 2 the script should exit. Instead what I get is this;
test is not 1
something went wrong
if test is not 1 it should not print this
I am new to python but not to scripting/coding. I have searched all over the place and every answer is simply "use sys.exit()"
Well it seems that sys.exit() has what seems to be unexpected behaviour when it is contained within try/except. If I remove the try it behaves as expected
Is this normal behaviour? If so is there a way to hard exit the script with out it continuing to execute into the exception block when test = 2?
Note: this is sample code that is a simplified version of the logic I am intending to use in another script. The reason the try/except is there is because testfunc() will be called using a variable and I want to catch the exception if an invalid function name is provided
Thanks in advance
Edit: I have also tried quit(), exit(), os._exit() and raise SystemExit