Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.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_Try Except - Fatal编程技术网

对所有捕获的异常执行公共操作的Python方式

对所有捕获的异常执行公共操作的Python方式,python,try-except,Python,Try Except,如果我有以下结构: try: do_something_dangerous() except Exception1: handle_exception1() handle_all_exceptions() except Exception2: handle_exception2() handle_all_exceptions() ... 如果我不想在除之外的每一个子句中调用处理所有异常,因为我有很多异常,那么调用处理所有异常的最python方式是什么?也

如果我有以下结构:

try:
    do_something_dangerous()
except Exception1:
    handle_exception1()
    handle_all_exceptions()
except Exception2:
    handle_exception2()
    handle_all_exceptions()
...

如果我不想在除之外的每一个
子句中调用
处理所有异常
,因为我有很多异常,那么调用
处理所有异常的最python方式是什么?也许有一种简单的方法可以确定异常是否发生在
finally
子句中?

我认为最简单的方法是嵌套try语句:

try:
   try:
        do_something_dangerous()
    except Exception1:
        handle_exception1()
        raise
    except Exception2:
        handle_exception2()
        raise
except Exception:
    handle_all_exceptions()
raise
将重新引发异常

def handle_all_exceptions(err):
    # common exception handling
    err_handling[type(err)]()
另一个选项是捕获所有异常并自行调度,而不是使用
try
语句:

try:
    do_something_dangerous()
except Exception as e:
    if isinstance(e, Exception1):
        handle_exception1()
    if isisntance(e, Exception2):
        handle_exception2()
    handle_all_exceptions()

我认为您还可以检查异常的类型。然而,我不知道这是否是最具Python风格的方式:

编辑:查看文档,似乎没有一种最符合Python的方式。如何处理函数中不同类型的异常取决于您自己

由于您不想检查实例或类似实例,这里有另一种可能的实现方式。虽然列表实现确实不好,但它在没有嵌套try语句的情况下解决了这个问题。您也可以使用布尔语句或类似语句,但是对于列表,您仍然可以访问错误对象以进行进一步处理

ex = []
try:
    do_something_dangerous()
except Exception1 as e:
    ex.append(e)
    handle_exception1()
except Exception2 as e:
    ex.append(e)
    handle_exception2()
finally:
    if ex: handle_all_exceptions(ex)

您可以首先定义一个映射,将处理函数与相应的异常关联起来:

err_handling = {
   Exception1: handle_exception1
   Exception2: handle_exception2
   # ...
}
然后,您可以接受引发的异常作为
handle\u all\u exceptions
函数的参数,并使用处理映射和引发异常的类型将其添加到特定处理中

def handle_all_exceptions(err):
    # common exception handling
    err_handling[type(err)]()
这样做可以用一种简单的方式处理异常:

try:
    do_something_dangerous()
except Exception as err:
    handle_all_exceptions(err)

我发现了一些棘手的解决方案,没有
类型
实例
,除了
子句之外,在每个
子句中都有大量的
提升
。也许这不是最具蟒蛇风格的,但至少很有趣:

try:
    do_something_dangerous()
except:
    try:
        raise
    except Exception1:
        handle_exception1()
    except Exception2:
        handle_exception2()
    handle_all_exceptions()

为什么不使用除异常之外的
作为ex'?这将捕获所有可能的异常。使用对象
ex`,或者不管你怎么称呼它,你都可以决定接下来要做什么。因此,如果isinstance(ex,Exception1),我需要编写
:handle_Exception1()
。似乎不是Pythonic。@Sanyash这大概就是
except
子句将要做的-根据您提供的类型顺序检查异常的类型。好的,所有答案都很好,但不要100%解决我的问题。我希望有一种方法不使用
type(exc)
isinstance
@带有
raise
的nosklo建议可能是最好的,但它需要两个
try
子句。@Sanyash为什么要限制type或isinstance的使用?还要对
Exception1
Exception2
的子类使用
isinstance
而不是
type(…)=