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