当前位置:首页 » 编程语言 » python中e

python中e

发布时间: 2023-04-04 03:06:26

python 中的e.message

最近发现还有人在处理Exception的时候还在使用 e.message 来输出异常(错误)信息。

程序中的一个举例(日志):

现在Python的最新版本已经是 python2.7.13/python3.6.2, 而 e.message 则是python2.5.x 的语法了。

PEP 352 -- Required Superclass for Exceptions
https://www.python.org/dev/peps/pep-0352/
简介:

DeprecationWarning: BaseException.message has been deprecated as of Python 2.6 exception. class , exception.message,
https://stackoverflow.com/questions/13063212/deprecationwarning-baseexception-message-has-been-deprecated-as-of-python-2-6-e

简介:

Proper way to declare custom exceptions in modern Python?
https://stackoverflow.com/questions/1319615/proper-way-to-declare-custom-exceptions-in-modern-python/26938914#26938914

BaseException.message deprecated in Python 2.6

② python except中的e是什么意思

这个e是异常类的一个实例,如果我们完整地解释这个问题,我觉得还是从Python的自定义异常类说起比较好。

假如,我们现在自定义一个简单的异常类:
class MyError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)

我们抛这个异常的时候可以这么写:
try:
raise MyError(2*2)
except MyError as e:
print 'My exception occurred, value:', e.value

我们在捕获这个异常之后假如需要访问TA的一些属性怎么办,这个时候就可以使用as关键字
所以,这里的e是前面MyError类的一个instance,我们可以直接访问他的value,也就是你看到的e.value

热点内容
java返回this 发布:2025-10-20 08:28:16 浏览:644
制作脚本网站 发布:2025-10-20 08:17:34 浏览:935
python中的init方法 发布:2025-10-20 08:17:33 浏览:631
图案密码什么意思 发布:2025-10-20 08:16:56 浏览:820
怎么清理微信视频缓存 发布:2025-10-20 08:12:37 浏览:730
c语言编译器怎么看执行过程 发布:2025-10-20 08:00:32 浏览:1065
邮箱如何填写发信服务器 发布:2025-10-20 07:45:27 浏览:298
shell脚本入门案例 发布:2025-10-20 07:44:45 浏览:159
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:849
python股票数据获取 发布:2025-10-20 07:39:44 浏览:762