1

The code

def Recursion():
    try:
        raise RecursionError()
    except RecursionError:
        Recursion()

Recursion()

crashes.

I am aware that this is a recursive function, but shouldn't Python just raise an StackError, instead of just crashing? If someone could explain this, I would be very grateful!

6
  • No, it raises an RecursionError and the way you handle it makes it actually worse. Commented Oct 5, 2021 at 7:22
  • It does raise RecursionError: maximum recursion depth exceeded while calling a Python object. Commented Oct 5, 2021 at 7:24
  • @kubatucka: That's not what I get when I try it. Commented Oct 5, 2021 at 7:56
  • I have no idea what ideone does. Run it a terminal. Commented Oct 5, 2021 at 7:57
  • @kubatucka: I still get Fatal Python error: _Py_CheckRecursiveCall: Cannot recover from stack overflow. in a terminal. Commented Oct 5, 2021 at 8:00

2 Answers 2

2

Something similar has been reported as a bug: https://bugs.python.org/issue42509, with someone commenting that

Catching a RecursionError might be fine sometimes, but the issue is that Program 1 catches a RecursionError and then keeps recursing more rather than stopping.

I think it might have to be the responsibility of the Python user to make sure that if a RecursionError is to be caught, that the program can recover without making things much worse. It's my understanding that the extra buffer of +50 is to make sure that the programmer has room to stop the overflow and do any necessary cleanup [1].

If no attempt is made to clean up, then it seems reasonable to me that Python should crash, unless there's some idea of what could happen that I'm missing. The interpreter could allow arbitrary recursion during the cleanup until the C stack overflows, but that sort of defeats the point of the recursion checker. It could raise some new ExtraSuperRecurionError, but that doesn't fix anything: what if that error is caught ;) ?

https://bugs.python.org/msg382128

Assuming this is a minimally reproducible toy example for a real issue you're facing, you should do:

def Recursion():
    try:
        raise Exception()
    except RecursionError:
        raise
    except Exception:
        Recursion()

Recursion()
Sign up to request clarification or add additional context in comments.

Comments

0

The RecursionError that you see is not the one you manually raise. It does not occur within the try-block and therefore is not caught:

def Recursion():
    try:
        raise RecursionError()  # does not happen here ...
    except RecursionError:
        Recursion()  # ... but here where it goes unhandled

2 Comments

That doesn't answer why it hard-crashes with "Fatal Python error: Cannot recover from stack overflow" and not a RecursionError.
@Patrick: The recursive call to Recursion is inside an except block, not a try. If it hits an actual stack overflow, it's not clear why the code doesn't just hit a RecursionError when it tries to call Recursion, then immediately end because there are no active try blocks anywhere on the stack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.