Python 尝试“除其他外”最终返回“除其他外”选项?
我正在开发一个可以引发多个异常的函数。我想在except块中处理这些Python 尝试“除其他外”最终返回“除其他外”选项?,python,python-2.7,exception,Python,Python 2.7,Exception,我正在开发一个可以引发多个异常的函数。我想在except块中处理这些异常,然后返回带有自定义消息和回溯的结果。问题是最终是有保证的,所以我不能返回else块中的任何内容 如果引发了某些异常,则此代码有效,但如果没有异常,则此代码无效。在这种情况下,我只想返回{'success':True} 因此,从这个代码: def foo(): try: #some code return {'success':True} except FirstExcepti
异常
,然后返回带有自定义消息和回溯的结果。问题是最终
是有保证的,所以我不能返回else
块中的任何内容
如果引发了某些异常
,则此代码有效,但如果没有异常
,则此代码无效。在这种情况下,我只想返回{'success':True}
因此,从这个代码:
def foo():
try:
#some code
return {'success':True}
except FirstException:
tb = traceback.format_exc()
msg = 'There was FirstExc'
return {'success':False,
'tb':tb,
'msg':msg}
except SecondException:
tb = traceback.format_exc()
msg = 'There was SecondExc'
return {'success':False,
'tb':tb,
'msg':msg}
...
我不想重复返回的内容
。我试过:
def foo():
try:
pass
except FirstException:
tb = traceback.format_exc()
msg = 'There was FirstExc'
except SecondException:
tb = traceback.format_exc()
msg = 'There was SecondExc'
else:
return {'success':True}
finally:
return {'success':False,
'tb':tb,
'msg':msg}
你知道怎么做吗?我知道我可以将return{'success':True}
放入try
块中,最后移除和else
块,并将return{'success':False,'tb':tb,'msg':msg}
添加到每个块中,除了
块之外,还有许多块,因此代码会重复多次
还有其他选择吗?不要使用finally
块。您只想为异常返回False
,否则返回True
;在finally
中返回始终适用于这两种情况
您可以从除
套件之外的两个套件返回,也可以合并套件
从这两方面返回会导致更多的重复:
def foo():
try:
pass
except FirstException:
tb = traceback.format_exc()
msg = 'There was FirstExc'
return {'success':False,
'tb':tb,
'msg':msg}
except SecondException:
tb = traceback.format_exc()
msg = 'There was SecondExc'
return {'success':False,
'tb':tb,
'msg':msg}
else:
return {'success':True}
您可以将除
之外的套件组合为一个套件:
def foo():
try:
pass
except (FirstException, SecondException) as e:
tb = traceback.format_exc()
exception_type = 'FirstExc' if isinstance(e, FirstException) else 'SecondExc'
msg = 'There was {}'.format(exception_type)
return {'success':False,
'tb':tb,
'msg':msg}
else:
return {'success':True}
另一个选项是首先构建返回值,然后根据需要添加信息:
def foo():
result = {'success': True}
try:
pass
except FirstException:
tb = traceback.format_exc()
msg = 'There was FirstExc'
result = {'success': False, 'tb': tb, 'msg': msg}
except SecondException:
tb = traceback.format_exc()
msg = 'There was SecondExc'
result = {'success': False, 'tb': tb, 'msg': msg}
finally:
return result
但是,这与except-suite选项中的返回
并没有太大区别
def foo():
success = False
try:
pass
except FirstException:
tb = traceback.format_exc()
msg = 'There was FirstExc'
except SecondException:
tb = traceback.format_exc()
msg = 'There was SecondExc'
else:
success = True
finally:
return {'success':success,
'tb':tb,
'msg':msg} if not success else {'success':success}
备选方案:
def foo():
result = {"success": False}
try:
pass
except FirstException:
result['tb'] = traceback.format_exc()
result['msg'] = 'There was FirstExc'
except SecondException:
result['tb'] = traceback.format_exc()
result['msg'] = 'There was SecondExc'
else:
result['success'] = True
finally:
return result
上述内容略有变化:
def foo():
result = {"success": False}
try:
pass
except FirstException:
result.update({'tb': traceback.format_exc(), 'msg': 'There was FirstExc'})
except SecondException:
result.update({'tb': traceback.format_exc(), 'msg': 'There was SecondExc'})
else:
result['success'] = True
finally:
return result
条件表达式使其相当不可读。如果不成功,只需使用:return{…}
,然后在下一行使用return{…}
。无需使用否则:
。将引发NameError@brunodesthuilliers:不会的。如果success
为True
,则条件表达式根本不会执行第一部分。但是,由于在最后一行使用了条件表达式,所以很难看到这一点。