Maximum recursion depth exceeded

I have created a program that uses recursion to solve simple mazes. In the case of a rather complex maze, I get the maximum recursion depth error. I have searched for this error on this website and read the threads, so I believe I have a general understanding of what is going on.

Unlike other streams I've seen, I am not trying to increase the recursion limit. sys.setrecursionlimit () is not what I'm looking for. I would like to be able to deal with the overflow and instead of crashing the program will print a message ( print("Sorry but this maze solver was not able to finish analyzing the maze due to recursion limits)

) and close.

I am aware of the use of try and exception to handle errors, but I'm not sure if I can include that to handle the maximum recursion depth error.

+3


source to share


1 answer


The recursion depth error is another exception; you can catch RecursionError

exception
(Python 3.5 or newer):

try:
    solveMaze(maze)
except RecursionError as re:
    print('Sorry but this maze solver was not able to finish '
          'analyzing the maze: {}'.format(re.args[0]))

      

I have included the error message attached to the runtime exception; for recursion error that maximum recursion depth exceeded

.

If you need to support Python versions before 3.5, you can catch the base class RuntimeError

. If you're worried about run-time errors that are not recursion depth errors, you can evaluate the value .args[0]

:



try:
    solveMaze(maze)
except RuntimeError as re:
    if re.args[0] != 'maximum recursion depth exceeded':
        # different type of runtime error
        raise
    print('Sorry but this maze solver was not able to finish '
          'analyzing the maze: {}'.format(re.args[0]))

      

Demonstration of options:

>>> def infinity(): return infinity()
... 
>>> try:
...     infinity()
... except RecursionError as re:
...     print('Oopsie: {}'.format(re.args[0]))
... 
Oopsie: maximum recursion depth exceeded
>>> def alter_dict_size():
...     dct = {'foo': 'bar'}
...     for key in dct:
...         del dct['foo']
... 
>>> try:
...     alter_dict_size()
... except RuntimeError as re:
...     print('Oopsie: {}'.format(re.args[0]))
... 
Oopsie: dictionary changed size during iteration
>>> try:
...     infinity()
... except RuntimeError as re:
...     if re.args[0] != 'maximum recursion depth exceeded':
...         raise
...     print('Oopsie: {}'.format(re.args[0]))
... 
Oopsie: maximum recursion depth exceeded
>>> try:
...     alter_dict_size()
... except RuntimeError as re:
...     if re.args[0] != 'maximum recursion depth exceeded':
...         raise
...     print('Oopsie: {}'.format(re.args[0]))
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "<stdin>", line 3, in alter_dict_size
RuntimeError: dictionary changed size during iteration

      

Resizing the dictionary also throws an exception RuntimeError

, but testing the resulting exception message allows you to differentiate.

+3


source







All Articles