Reputation:
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
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
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