How to determine max. recursion depth exceeded exception in python?

try: recursive_function() except RuntimeError e: # is this a max. recursion depth exceeded exception? 

How do you know when the maximum recursion depth is reached?

+7
source share
1 answer

You can look inside the exception itself:

 >>> def f(): ... f() ... >>> try: ... f() ... except RuntimeError as re: ... print re.args, re.message ... ('maximum recursion depth exceeded',) maximum recursion depth exceeded 

I do not think that you can distinguish between this and something, just pretending to be an exception as an exception with excess recursion (Runtime). message deprecated, so args is probably the best option and compatible with Python-3.


Update: There is a special RecursionError in Python 3.5 that you can catch instead.

+8
source

All Articles