在Python中,一条try
语句可以有多条except
语句吗?例如:
try:
#something1
#something2
except ExceptionType1:
#return xyz
except ExceptionType2:
#return abc
发布于 2011-05-23 18:13:07
是的,这是可能的。
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“关键字用于将错误赋给变量,以便稍后在代码中可以更彻底地调查错误。还要注意,在Python3中需要对三重异常用例使用括号。
https://stackoverflow.com/questions/6095717
复制相似问题