Skip to Content
Python 技術手冊 第三版
book

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate to advanced content levelIntermediate to advanced
856 pages
17h 17m
Chinese
GoTop Information, Inc.
Content preview from Python 技術手冊 第三版
例外物件
|
183
個引數。如果有任一個引數是空的,此函式就會提出
ValueError
,而非像
串列概括式(list comprehension)一般會做的那樣單純回傳一個空串列。
只檢查你需要檢查的
cross_product
沒必要檢查
seq1
seq2
是否可迭代的
iterable):如果有任一個不是,串列概括式本身就會提
出適當的例外,通常是
TypeError
只要有一個例外被提出,不管是 Python 本身所提的,或你程式碼中一個
明確的
raise
述句所做的,都是由呼叫者來判斷要處理它(以合適的
try/
except
述句)或是讓它在呼叫堆疊中往上傳播。
別把
raise
用於重複、多餘的錯誤檢查
只把
raise
用來提出一般來說是正常,但你的規格定義為
錯誤的附加例外。別使用
raise
來重複 Python 已經為你隱
含地進行的錯誤檢查。
例外物件
例外是
BaseException
的子類別的實體。所有的例外都有
args
屬性,也就
是用來創建該實體的引數所構成的元組。這個特定的錯誤資訊適合用於診
斷或復原。某些例外類別會解譯
args
並以具名屬性(named attributes)的
形式提供它們,以方便使用。
標準例外的階層架構
例外是
BaseException
的子類別的實體(只在 v2 中,為了回溯相容性,你
可以把某些其他的物件視為「例外」,但本書中不涵蓋這種傳統的複雜用
)。
例外類別的繼承結構很重要,因為它決定了哪些
except
子句處理
哪些例外。大多數的例外類別擴充的是
Exception
類別:然而,
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

測試驅動開發|使用 Python

測試驅動開發|使用 Python

Harry J.W. Percival
Linux 内核观测技术BPF

Linux 内核观测技术BPF

David Calavera, Lorenzo Fontana

Publisher Resources

ISBN: 9789864766819