【问题标题】:How to get Exception arguments?如何获取异常参数?
【发布时间】:2022-11-24 17:17:08
【问题描述】:

我试图在不支持的 python 3.6.5 中找到一种方法来执行此操作

try:
     c=1/0
     print (c)
except ZeroDivisionError, args:
     print('error dividing by zero', args)

它说python 3.6.5不支持这种类型的语法 那么有没有办法获取异常的参数呢?

【问题讨论】:

    标签: python python-3.6


    【解决方案1】:

    怎么样:

    try:
         c=1/0
         print (c)
    except ZeroDivisionError as e:
         print('error dividing by zero: ' + str(e.args))
    

    逗号现在用于except多种类型的异常,它们需要放在括号中,例如:

    try:
        c = int("hello")
        c = 1 / 0
        print(c)
    except (ZeroDivisionError, ValueError) as e:
        print('error: ' + str(e.args))
    

    【讨论】:

      猜你喜欢
      • 2014-01-12
      • 2012-06-13
      • 2017-08-10
      • 2012-03-19
      • 1970-01-01
      • 2013-05-20
      • 2010-10-19
      • 2021-08-19
      • 1970-01-01
      相关资源
      最近更新 更多