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

我刚刚开始我的 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.


喵喵时光机
浏览 144回答 1
1回答

一只甜甜圈

在处理异常时,可以将其赋值给一个变量,例如:    except AssertionError as aerr:        saved_exception = aerr您以后可以访问它,例如:print(saved_exception)对于您的代码,这为您提供了没有两个变量的选项,而isError不仅仅是使用saved_exception = None和稍后测试if saved_exception is not None: ...不确定为以后保存异常(将其用作传递信息的通用接口)有多大用处。也许值得重新考虑一下。N Chauhan在评论中也提出了一个很好的观点,即AssertionError不是非常适合用来传达此类信息的异常。对于您更新的问题。如果你想打印回溯是你会在引发异常时看到它,最直接的可能是保存异常信息并使用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__对于第一个示例中的保存异常)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python