These days, I am very deeply studying Python exception handling functions. I ran into exception SystemExit. After reading about this from the official Python docs , I realized the question, what exactly will happen when I finish the Python script by pressing Ctrl+ c?
lets take this example code:
def func1(a,b):
print "func1: "+str(a/b)
def func2(a,b):
print "func2: "+str(a/b)
if __name__=="__main__":
import random
count=0
for i in range(1000000):
count=count+1
print "count: "+str(count)
try:
func1(random.randint(-2,3),random.randint(-2,3))
except KeyboardInterrupt:
raise
except:
print "error in func1"
try:
func2(random.randint(-2,3),random.randint(-2,3))
except KeyboardInterrupt:
raise
except:
print "error in func2"
print "\n"
In this code example, I caught KeyboardInterrupt, so I can stop my script by pressing Ctrl+ c. Should I catch SystemExittoo to make this code more mature? if so, why? in fact, this question is the source of my main question, which appears in the title. therefore do not consider that I ask two different questions in one post.