考虑以下代码:
有什么区别吗? 什么时候应该使用
def f(x):
if x < 10:
return Exception("error")
else:
raise Exception("error2")
if __name__ == "__main__":
try:
f(5) # f(20)
except Exception:
print str(Exception)
有什么区别吗? 什么时候应该使用
return Exception
,什么时候应该使用raise
?
return
是什么吗?我建议参考Python官方教程。 - TigerhawkT3