Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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:嵌套的try-catch处理_Python_Nested_Try Catch - Fatal编程技术网

Python:嵌套的try-catch处理

Python:嵌套的try-catch处理,python,nested,try-catch,Python,Nested,Try Catch,目前我有一些这样的代码 try: somecode except Exception as Error: fallbackcode 现在我想在回退代码中添加另一个回退 在python中创建嵌套的try/catch的最佳实践是什么 try: somecode except Exception as Error: try: fallbackcode except Exception as Error: nextfallback

目前我有一些这样的代码

try:
    somecode
except Exception as Error:
    fallbackcode
现在我想在回退代码中添加另一个回退 在python中创建嵌套的try/catch的最佳实践是什么

try:
    somecode
except Exception as Error:
    try:
        fallbackcode
    except Exception as Error:
        nextfallbackcode

产生预期错误

您应该能够准确地执行嵌套的try/except块,并在问题中实现它

下面是另一个例子:

import os

def touch_file(file_to_touch):
    """
    This method accepts a file path as a parameter and
    returns true/false if it successfully 'touched' the file
    :param '/path/to/file/'
    :return: True/False
    """    
    file_touched = True

    try:
        os.utime(file_to_touch, None)
    except EnvironmentError:
        try:
            open(file_to_touch, 'a').close()
        except EnvironmentError:
            file_touched = False

    return file_touched

您应该能够准确地在问题中实现嵌套的try/except块

下面是另一个例子:

import os

def touch_file(file_to_touch):
    """
    This method accepts a file path as a parameter and
    returns true/false if it successfully 'touched' the file
    :param '/path/to/file/'
    :return: True/False
    """    
    file_touched = True

    try:
        os.utime(file_to_touch, None)
    except EnvironmentError:
        try:
            open(file_to_touch, 'a').close()
        except EnvironmentError:
            file_touched = False

    return file_touched

如果所有回调和回退都具有相同的API接口,则可以使用循环重写逻辑

for code in [somecode, fallbackcode, nextfallbackcode]:
    try:
        code(*args, **kwargs)
        break
    except Exception as Error:
        continue
else:
    raise HardException

这将是替代多层嵌套异常块的首选方法。

如果所有回调和回退都具有相同的API接口,则可以使用循环重写逻辑

for code in [somecode, fallbackcode, nextfallbackcode]:
    try:
        code(*args, **kwargs)
        break
    except Exception as Error:
        continue
else:
    raise HardException

这将是首选方法,而不是多层嵌套异常块。

您可以这样更改它:

try:
    try:
        somecode
    except Exception as Error:
        fallbackcode
except Exception as Error:
    nextfallbackcode

你可以这样改变它:

try:
    try:
        somecode
    except Exception as Error:
        fallbackcode
except Exception as Error:
    nextfallbackcode

您可以使用函数来处理错误:

def func0():
    try:
        somecode
    except:
        other_func()

def func1():
    try:
        somecode
    except:
        func0()

您可以使用函数来处理错误:

def func0():
    try:
        somecode
    except:
        other_func()

def func1():
    try:
        somecode
    except:
        func0()

不,如果你做得对的话,不会产生编辑错误。我们不能告诉你们你们做错了什么,因为你们并没有费心包括一个。你们是对的,问题是一个早期的缩进错误产生了意图错误不,若你们做对了就不会。我们不能告诉你你做错了什么,因为你没有费心包括一个。你是对的,问题是一个更早的缩进错误。你忘了休息。你忘了休息。