[英]Python: One Try Multiple Except
在Python中,是否可以為一個try
語句使用多個except
語句? 如 :
try:
#something1
#something2
except ExceptionType1:
#return xyz
except ExceptionType2:
#return abc
對的,這是可能的。
try:
...
except FirstException:
handle_first_one()
except SecondException:
handle_second_one()
except (ThirdException, FourthException, FifthException) as e:
handle_either_of_3rd_4th_or_5th()
except Exception:
handle_all_other_exceptions()
請參閱: http : //docs.python.org/tutorial/errors.html
“as”關鍵字用於將錯誤分配給變量,以便稍后可以在代碼中更徹底地調查錯誤。 另請注意,python 3中需要三重異常情況的括號。此頁面包含更多信息: 在一行中捕獲多個異常(塊除外)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.