为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

喵喵时光机 2022-12-27 14:46:12
我刚刚开始我的 python 学习之旅,需要一些帮助以正确的方式引发异常。考虑一个循环遍历列表并执行任务的代码块。如果发生异常,则继续执行程序。并执行其余代码。在程序结束时引发异常并使用非零代码系统化应用程序。这个想法是程序应该继续执行所有任务,但退出时使用非 0 代码供外部应用程序跟踪和报告。save_exc_info = Nonedef 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 codeexcept 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.
查看完整描述

1 回答

?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

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


    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__对于第一个示例中的保存异常)。


查看完整回答
反对 回复 2022-12-27
  • 1 回答
  • 0 关注
  • 82 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号