Skip to content
Advertisement

What is the proper way to exit the whole script?

def fu():
    try:
        print('hello')
        fu()
    except:
        print('last hello')
        exit()

fu()

So it should raise a recursion error after some time and when it does that I want to exit the whole code.

So what I am trying to get is:

hello
hello
...
last hello

But it gives something like:

hello
hello
...
last hello
last hello

So when it raises that error I want to do something and that’s all, just quit, no more trying

Advertisement

Answer

You need to add the base Exception type to the except and it will work.

def fu():
    try:
        print('hello')
        fu()
    except Exception:
        print('last hello')
        exit()
fu()
Advertisement