在Python中使用try/except塊時,是否有建議將它委託給可能引發異常,或在父函數中捕獲它的方法?python中異常處理的位置
例如,以下哪項是首選?
def my_function():
s = something.that.might.go_wrong()
return s
def main():
try:
s = my_function()
except Exception:
print "Error"
或
def my_function():
try:
s = something.that.might.go_wrong()
return s
except Exception:
print "Error"
def main():
s = my_function()
PEP 8似乎是安靜的事,我似乎找到了兩種情況的例子隨處可見。
謝謝@Amber。你在考試能力方面做得很好,這與AlokThakur的答案很好地結合在一起。 – mcy