处理捕获的异常并在 try 块之外引发异常的正确方法是什么

what is the correct way to handle a caught exception and raise it outside of try block

我刚刚开始我的 python 学习之旅,需要一些帮助以正确的方式引发异常。

考虑一个循环遍历列表并执行任务的代码块。如果发生异常,则继续执行程序。并执行其余代码。在程序结束时引发异常并使用非零代码系统化应用程序。这个想法是程序应该继续执行所有任务,但退出时使用非 0 代码供外部应用程序跟踪和报告。

save_exc_info = None

def numMatcher(numbers):
    try:
        if numbers != 2:

            print('number match ' + str(numbers))

        else:
            raise ValueError('Number not in list. Will be logged for troubleshooting')  # raise exception and log it

    except ValueError as veer:  # exception 1 caught and saved
        save_exc_info = sys.exc_info()


    except (IOError, OSError) as ioerr:  # exception 2 caught and saved
        save_exc_info = sys.exc_info()


try:
    print('Next step')  # Perform rest of the tasks in the code

except Exception as excp:  # exception 3 caught and saved
    save_exc_info = sys.exc_info()

print('final step')

numlist = [1, 2, 3]

for numbers in numlist:
    numMatcher(numbers)

if save_exc_info is not None:
    traceback.print_exception(*save_exc_info)  # how to return the right exception and print?
    sys.exit(1)  # At the end of the program, exit with non zero code as there was an exception in the program.

在处理异常时,可以将其赋值给一个变量,例如:

    except AssertionError as aerr:
        saved_exception = aerr

您以后可以访问的内容,例如:

print(saved_exception)

对于您的代码,这为您提供了一个没有两个变量的选项,而不是 isError 只需使用 saved_exception = None 并稍后测试 if saved_exception is not None: ...

不确定为以后保存异常(将其用作传递信息的通用接口)有多大用处。也许值得重新考虑一下。

N Chauhan also made a good point int the comment about AssertionError 不是非常适合用于传达此类信息的异常。


针对您更新的问题。如果你想打印回溯是你会在引发异常时看到它,最直接的可能是保存 exception information and use print_exception() (或其 format_exception 朋友):

except ValueError:
    save_exc_info = sys.exc_info()            
...
traceback.print_exception(*save_exc_info) 

您也可以从/使用已保存的异常中提取相同的信息,*save_exc_info 也可以是:type(saved_exception), saved_exception, saved_exception.__traceback__(对于第一个示例中的已保存异常)。