Eva*_*611 162 python syntax exception-handling
在Python中,是否可以except为一个try语句提供多个语句?如 :
try:
#something1
#something2
except ExceptionType1:
#return xyz
except ExceptionType2:
#return abc
Run Code Online (Sandbox Code Playgroud)
var*_*tec 276
对的,这是可能的.
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()
Run Code Online (Sandbox Code Playgroud)
请参阅:http: //docs.python.org/tutorial/errors.html
"as"关键字用于将错误分配给变量,以便稍后可以在代码中更彻底地调查错误.另请注意,python 3中需要三重异常情况的括号.此页面包含更多信息:在一行中捕获多个异常(块除外)
| 归档时间: |
|
| 查看次数: |
91559 次 |
| 最近记录: |