一旦进入finally块,如何确定是否引发了异常? [英] How to determine if an exception was raised once you're in the finally block?

查看:406
本文介绍了一旦进入finally块,如何确定是否引发了异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦您进入finally子句,是否可以判断是否存在异常?像这样:

Is it possible to tell if there was an exception once you're in the finally clause? Something like:

try:
    funky code
finally:
    if ???:
        print('the funky code raised')

我正在尝试制作类似DRY的东西:

I'm looking to make something like this more DRY:

try:
    funky code
except HandleThis:
    # handle it
    raised = True
except DontHandleThis:
    raised = True
    raise
else:
    raised = False
finally:
    logger.info('funky code raised %s', raised)

我不喜欢它需要捕获一个您不打算处理的异常,而只是设置一个标志.

I don't like that it requires to catch an exception, which you don't intend to handle, just to set a flag.

由于某些

Since some comments are asking for less "M" in the MCVE, here is some more background on the use-case. The actual problem is about escalation of logging levels.

  • 时髦的代码是第三方,不能更改.
  • 故障异常和堆栈跟踪不包含任何有用的诊断信息,因此,在except块中使用logger.exception对此无济于事.
  • 如果引发了时髦的代码,那么我需要查看的一些信息已经记录在DEBUG级别.我们不能也不能够处理该错误,但是由于需要的信息在其中,因此希望升级DEBUG日志记录.
  • 大多数情况下,时髦的代码不会提高.我不想提升一般情况下的日志记录级别,因为它太冗长了.
  • The funky code is third party and can't be changed.
  • The failure exception and stack trace does not contain any useful diagnostic information, so using logger.exception in an except block is not helpful here.
  • If the funky code raised then some information which I need to see has already been logged, at level DEBUG. We do not and can not handle the error, but want to escalate the DEBUG logging because the information needed is in there.
  • The funky code does not raise, most of the time. I don't want to escalate logging levels for the general case, because it is too verbose.

因此,代码在日志捕获上下文下运行(该日志捕获上下文设置了自定义处理程序以拦截日志记录),并且一些调试信息会被追溯重新记录:

Hence, the code runs under a log capture context (which sets up custom handlers to intercept log records) and some debug info gets re-logged retrospectively:

try:
    with LogCapture() as log:
        funky_code()  # <-- third party badness
finally:
    # log events are buffered in memory. if there was an exception,
    # emit everything that was captured at a WARNING level
    for record in log.captured:
        if <there was an exception>:
            log_fn = mylogger.warning
        else:
            log_fn = getattr(mylogger, record.levelname.lower())
        log_fn(record.msg, record.args)

推荐答案

raised = True
try:
    funky code
    raised = False
except HandleThis:
    # handle it
finally:
    logger.info('funky code raised %s', raised)

考虑到添加有关选择日志级别的问题的其他背景信息,这似乎很容易适应预期的用例:

Given the additional background information added to the question about selecting a log level, this seems very easily adapted to the intended use-case:

mylog = WARNING
try:
    funky code
    mylog = DEBUG
except HandleThis:
    # handle it
finally:
    mylog(...)

这篇关于一旦进入finally块,如何确定是否引发了异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆