【问题标题】:Handling exceptions without try block in Python's interactive shell在 Python 的交互式 shell 中处理没有 try 块的异常
【发布时间】:2009-07-03 06:51:21
【问题描述】:

查看此问题的标题。我想玩最后一个命令中引发的异常。 _ 没有帮助我。有这样的吗?

【问题讨论】:

  • 您能否提供任何代码或错误回溯或任何我们可以阅读的内容以了解您在说什么?

标签: python command-line exception-handling interactive-mode


【解决方案1】:

这样做:

import sys
sys.exc_info()

它将为您提供有关异常的信息。它是一个包含异常类型、异常实例和回溯对象的元组。

【讨论】:

  • sys.last_value 在 3.0 中不起作用,sys.exc_info() 在 3.0 和 2.x 中起作用
  • sys.last_value 在 2.7 中有效,根据文档,它在 Python 3.3 中仍然存在。但是 sys.exc_info() 不能在 except 块之外工作。
【解决方案2】:

如果您的“交互式”发生在 Jupyter 中,请检查此 >Jupyter magic to handle notebook exceptions

真是太美了。

如果要添加声音> Jupyter / Colab : Play sound with any error in any cell + Play sound after completing long running cells

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    相关资源
    最近更新 更多