【问题标题】:Store exception body in variable将异常体存储在变量中
【发布时间】:2012-02-12 15:31:51
【问题描述】:

有没有办法执行try 语句并将错误主体作为变量返回?

var = ''
try:
    error generating code
except:
    var = exception_body

【问题讨论】:

    标签: python exception try-catch


    【解决方案1】:

    是的,使用exceptas 语法:

    try:
        raise Exception("hello world")
    except Exception as x:
        print(x)
    

    在早期版本的 Python 中,这会写成 except Exception, x:,您可能会不时看到它。

    【讨论】:

      猜你喜欢
      • 2013-10-20
      • 1970-01-01
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      • 2013-04-06
      • 2016-03-31
      • 2016-02-12
      • 2021-06-22
      相关资源
      最近更新 更多