在Python中,除了'e'之外的'Exception'是什么意思? [等候接听]

问题描述 投票:2回答:1

异常处理的典型结构如下:

try:
    pass
except Exception, e:
    raise
else:
    pass
finally:
    pass

我可以知道except Exception, e:orexcept Exception as e:是什么意思吗?通常我会使用print (e)来打印错误消息,但我想知道程序为生成e做了什么。

如果我以另一种方式构建它(下面),它会怎么样?

except Exception:
    e = Exception.something

该方法应该取代something

try下的代码体没有异常时,程序将执行else下的代码。但是,finally在这做什么?

python python-2.7 python-3.x exception-handling semantics
1个回答
13
投票

except Exception as eexcept Exception, e(仅限Python 2.x)意味着它捕获Exception类型的异常,并且在except:块中,引发的异常(实际对象,而不是异常类)绑定到变量e

至于finally,它是一个总是被执行的块,无论发生什么,在except块之后(如果引发异常)但总是在触发范围之外的任何其他事件之前(例如returncontinueraise) 。

© www.soinside.com 2019 - 2024. All rights reserved.