當前位置:首頁 » 編程語言 » 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-07-02 05:14:51 瀏覽:185
長安歐尚a800有哪些配置 發布:2025-07-02 04:43:57 瀏覽:873
資料庫語句轉換 發布:2025-07-02 04:27:43 瀏覽:62
蘋果手機登錄微信如何儲存密碼 發布:2025-07-02 04:22:05 瀏覽:817
現場解壓 發布:2025-07-02 04:14:37 瀏覽:668
ad域控伺服器長得什麼樣 發布:2025-07-02 04:14:32 瀏覽:159
企業如何高效率地配置資源 發布:2025-07-02 04:14:30 瀏覽:632
python遞增 發布:2025-07-02 04:12:15 瀏覽:419
租國際伺服器有什麼優勢 發布:2025-07-02 04:12:06 瀏覽:838
瓢蟲編程工具 發布:2025-07-02 04:10:48 瀏覽:714