是否有办法确定代码块中可能引发的所有可能的异常? (例如,某些逻辑建议根据代码块捕获某些特定的异常,而不是仅仅引发该异常,这种逻辑太笼统了。
try:
m = check_output(["dd", "--version"]).decode()
ver_line = m.split('\n')[0]
ver = ver_line.split(' ')
if float(ver[2]) >= 8.24:
logger.info("coreutils version: {}, required >= 8.24".format(ver[2]))
else:
logger.warn("coreutils version: {}, required >= 8.24."
"Please ensure that the right version is installed".format(ver[2]))
sys.exit(1)
except Exception:
logger.warn("could not determine coreutils version, required >= 8.24")
return
答案 0 :(得分:-1)
将Exceptions设置为e,您可以打印任何异常 您通过使用
来确定错误type(e) == IndexError
然后比较它的值
a = ['a', 'b', 1, 3, 4, 5, 6]
for i in range(0, 20):
try:
print(int(a[i]))
except Exception as e:
if type(e) == IndexError:
pass
else:
print("ERROR",e)