當前位置:首頁 » 編程語言 » 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

熱點內容
招聘慢走絲編程 發布:2025-10-16 00:58:19 瀏覽:384
android線程時間 發布:2025-10-16 00:52:33 瀏覽:583
dos命令拷貝文件夾 發布:2025-10-16 00:32:47 瀏覽:166
地下城盟約安卓版哪個伺服器人多 發布:2025-10-16 00:29:58 瀏覽:179
編程gif 發布:2025-10-16 00:19:50 瀏覽:377
如何開啟微信打開密碼 發布:2025-10-16 00:18:37 瀏覽:718
如何做能讓好友進來的伺服器 發布:2025-10-15 23:50:03 瀏覽:123
linux安裝setuptools 發布:2025-10-15 23:47:36 瀏覽:404
下載軟體為什麼找不到伺服器 發布:2025-10-15 23:39:14 瀏覽:376
如何導入oracle資料庫 發布:2025-10-15 23:39:01 瀏覽:363