user13566332
user13566332

Reputation:

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

Upvotes: 1

Views: 86

Answers (2)

vivek kumar
vivek kumar

Reputation: 314

I believe you should catch particular 'RecursionError' instead of all exceptions.

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

Upvotes: 3

CallMePhil
CallMePhil

Reputation: 1657

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()

Upvotes: 0

Related Questions