Python 可以运行 while 循环直到 RuntimeError : maximum recursion depth exceeded while calling a Python object

标签 python recursion error-handling runtime runtime-error

我正在运行一个递归循环以实现对某些事物的最大优化,并且正在达到代码命中 RuntimeError: maximum recursion depth exceeded while calling a Python object 的点。这是预料之中的,但我想在遇到该错误之前停止代码,以便我可以在该点查看我的数据。

是否可以使用 while 循环来表达类似的内容?类似于“嘿,运行此循环,直到达到最大递归深度。一旦达到,停止并返回。”

感谢您的帮助!

最佳答案

您可以执行以下操作

def recurse():
  try:
    recurse()
  except RuntimeError as e:
    if e.message == 'maximum recursion depth exceeded while calling a Python object':
      # don't recurse any longer
    else:
      # something else went wrong

http://docs.python.org/tutorial/errors.html

注意:找出最大递归深度错误的错误编号并检查它而不是错误字符串可能是值得的。

关于Python 可以运行 while 循环直到 RuntimeError : maximum recursion depth exceeded while calling a Python object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12114590/

相关文章:

haskell - 此折叠实现中的错误

python - 将列表递归减 1

java - Haskell 完整的文本文件索引器

jquery - 如何使用rails bootstrap modal通过ajax提交表单

php - 如何将move_uploaded_file的错误记录到文件中

python - scipy.optimize 非线性约束

python - ReadDirectoryChangesW 阻止删除监视的目录

python - 如何找到元组列表中每个相似条目的平均值?

python - setuptools:测试我的额外功能

ruby-on-rails - Bugsnag 上的 ActionController::UnknownFormat