我试图找到一种方法在不支持的 python 3.6.5 中执行此操作
try:
c=1/0
print (c)
except ZeroDivisionError, args:
print('error dividing by zero', args)
它说 python 3.6.5 不支持这种类型的语法 那么有没有办法获取异常的参数呢?
最佳答案
怎么样:
try:
c=1/0
print (c)
except ZeroDivisionError as e:
print('error dividing by zero: ' + str(e.args))
逗号表示法现在用于异常(exception)
多种类型的异常,并且它们需要放在括号中,例如:
try:
c = int("hello")
c = 1 / 0
print(c)
except (ZeroDivisionError, ValueError) as e:
print('error: ' + str(e.args))
关于python - 如何获取异常参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50530645/