Python尝试-否则选项的预期用途是什么?else的条款try口供?
3 回答
慕标5832272
TA贡献1966条经验 获得超4个赞
else
try
使用ASER子句比向try子句添加额外代码更好,因为它避免意外捕获未被try保护的代码引发的异常.除了陈述。
IOError
try: operation_that_can_throw_ioerror()except IOError: handle_the_exception_somehow()else: # we don't want to catch the IOError if it's raised another_operation_that_can_throw_ioerror()finally: something_we_always_need_to_do()
another_operation_that_can_throw_ioerror()
operation_that_can_throw_ioerror
except
try
finally
else
第二个操作只有在没有例外的情况下才能运行, 它会在 finally
布洛克 任何 IOError
它不会在这里被抓到
扬帆大鱼
TA贡献1799条经验 获得超9个赞
else
try: from EasyDialogs import AskPassword # 20 other lines getpass = AskPasswordexcept ImportError: getpass = default_getpass
try: from EasyDialogs import AskPasswordexcept ImportError: getpass = default_getpasselse: # 20 other lines getpass = AskPassword
except
try: from EasyDialogs import AskPasswordexcept ImportError: getpass = default_getpass return False # or throw Exception('something more descriptive')# 20 other linesgetpass = AskPassword
注:
添加回答
举报
0/150
提交
取消