【发布时间】:2015-02-08 02:30:34
【问题描述】:
我有两个关于异常处理的问题。
Q1) 我有点不确定else 中的操作何时将在异常处理中执行。我不确定何时会执行 else 块,这不会出现在下面的代码中:
def attempt_float(SecPrice,diffprice):
try:
return float(SecPrice)
except:
return diffprice
else:
print "Did we succeed?"
print attempt_float('7','3')
Q2) 当我运行以下代码时:
def attempt_float(SecPrice,diffprice):
try:
return float(SecPrice)
except:
return diffprice
else:
print "Did we succeed?"
finally:
print "Yasdsdsa"
print attempt_float('7','3')
我不清楚为什么输出是:
Yasdsdsa
7.0
【问题讨论】:
-
你返回
try,所以你从来没有打过else -
Python try-else 的可能重复项
标签: python function exception exception-handling