import random
class StopRecursion(Exception):
pass
def some_recursive_function(depth=0):
print ("depth=%d" % depth)
# играем в русскую рулетку
if random.randint(1,6) == 4:
raise StopRecursion
some_recursive_function(depth+1)
try:
some_recursive_function()
except StopRecursion:
print ("Recursion stopped")