python - 如果没有抛出异常则执行

标签 python exception python-3.x try-catch finally

如果没有抛出异常,我想执行一些代码。

目前我正在这样做:

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

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

更新0

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

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

最佳答案

try:
   tmp = type, self.message_handlers[type](self, length - 1)
except Exception:
   pass #or handle error, or just "raise" to re-raise
else:
   self.last_recv_time = time.time()
   return tmp

关于python - 如果没有抛出异常则执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6911999/

相关文章:

python-3.x - 属性错误 : 'Map' object has no attribute 'Marker'

python - 用于管理线程的替代 python 库

python - 要列出的 Pandas 列名称

java - 在无法访问的 Java 线程中处理未捕获的异常

python-3.x - 传递变量 python 3 和 html

python - 在Python中没有指针的情况下更新JOSNObject中的值

python - 使用 groupby 和 last 拆分 Pandas Dataframe

python - 使用棋盘图案对 Pandas DataFrame 进行切片

exception - Sikuli、Java 和 java.lang.ThreadDeath 异常

c++ - 在 C++ 中抛出表达式重新排序?