如果没有抛出异常则执行 [英] Execute if no exception thrown

查看:44
本文介绍了如果没有抛出异常则执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果抛出异常,我想执行一些代码.

I have some code I want to execute if an exception is not thrown.

目前我正在这样做:

try:
    return type, self.message_handlers[type](self, length - 1)
finally:
    if not any(self.exc_info()):
        self.last_recv_time = time.time()

这个可以改进吗?这是最好的方法吗?

Can this be improved on? Is this the best way to do it?

可选的 else 子句在控制流出时执行try 子句的结尾.

The optional else clause is executed if and when control flows off the end of the try clause.

目前,控制从尽头流出",除非是异常或执行 return、continue 或 break 语句.

Currently, control "flows off the end" except in the case of an exception or the execution of a return, continue, or break statement.

推荐答案

你的代码表明你不想在异常发生时捕获它,所以为什么不简单地

Your code suggests that you don't want to catch the exception if it occurs, so why not simply

result = type, self.message_handlers[type](self, length - 1)
self.last_recv_time = time.time()
return result

(我错过了什么?)

这篇关于如果没有抛出异常则执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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