What happens exactly inside when I finish my Python script with Ctrl + c?

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)
    #some more functions

def func2(a,b):
    print "func2: "+str(a/b)
    #some more functions

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.

+4
4

SystemExit, exit() sys.exit() :

sys.exit([Arg])

Python. SystemExit, , finally try, , .

:

try:
    exit()
except SystemExit:
    print "caught"

, ( except:). , , . , , , , .

+4

, - ZeroDivisionError, :

import random

if __name__ == "__main__":
    for count in range(1000000):
        print "count:", count

        try:
            func1(random.randint(-2, 3),random.randint(-2, 3))
        except ZeroDivisionError:
            print "error in func1"

        try:
            func2(random.randint(-2, 3),random.randint(-2, 3))
        except ZeroDivisionError:
            print "error in func2"

        print "\n"
+1

, .

  • :

    , , python SIGINT KeyboardInterrupt.

  • :

    except:.

    if __name__=="__main__":
        try:
            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 Exception, e:
                            print "error in func1", e # or something...
                    try:
                            func2(random.randint(-2,3),random.randint(-2,3))
                    except Exception, e:
                            print "error in func2", e # or something...
    
                    print "\n"
    except Exception: 
        raise # any other "normal" exception.
    except: # Here it is ok, as you handle most exceptions above.
        pass
    

"" , , Exception. , ( ), Exception.

KeyboardInterrupt, SystemExit GeneratorExit.

+1

, , , , ( ):

  • , , ,
  • catch Exception, .

KeyboardInterrupt, SystemExit Exception, Python, ,

+1

Source: https://habr.com/ru/post/1524233/


All Articles