Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试“除其他外”最终返回“除其他外”选项?_Python_Python 2.7_Exception - Fatal编程技术网

Python 尝试“除其他外”最终返回“除其他外”选项?

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

我正在开发一个可以引发多个异常的函数。我想在except块中处理这些
异常
,然后返回带有自定义消息和回溯的结果。问题是
最终
是有保证的,所以我不能返回
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
,则条件表达式根本不会执行第一部分。但是,由于在最后一行使用了条件表达式,所以很难看到这一点。