我必須處理大量的try/except。我懷疑正確的做法。Python - Try/Except,什麼是好的,什麼是錯的?
選項1:
inst = Some(param1, param2)
try:
is_valid = retry_func(partial(inst.some_other), max_retry=1)
except RetryException, e:
SendMail.is_valid_problem(e)
if is_valid:
print "continue to write your code"
...
*** more code with try/except ***
...
選項2:
inst = Some(param1, param2)
try:
is_valid = retry_func(partial(inst.some_other), max_retry=1)
if is_valid:
print "continue to write your code"
...
*** more code with try/except ***
...
except RetryException, e:
SendMail.is_valid_problem(e)
在選項1,甚至是引發異常, 「is_valid」 將受到考驗,我不需要這個。
在選項2中,我認爲是正確的,但代碼看起來像一個「回調地獄」。
我應該選擇什麼樣的選項或什麼樣的選擇是正確的?
選項1是要走的路 – thefourtheye