在我的程序中,我有一個處理requests
調用的函數,並返回處理的調用或引發異常。這個函數被許多其他函數使用,但是我遇到的問題是如何處理可能會引發的異常。目前,它是建立像這樣(簡化):Python:有沒有辦法強制父函數返回?
def getFromAPI(url):
# create variable headers
r = requests.get(url, headers=headers)
if r.status_code == 404:
raise Exception("Error raised")
else:
#process data
return data
def functionone():
...
try:
data = getFromAPI(url)
except Exception as e:
return handleException(e)
#handles problems, returns exception-formatted data
...
# formatting data specific to functionone
return formatted_data
def functiontwo():
...
try:
data = getFromAPI(url)
except Exception as e:
return handleException(e)
#handles problems, returns exception-formatted data
...
# formatting data specific to functiontwo
return formatted_data
def functionthree():
...
#similar to functionone and functiontwo
雖然我不認爲這是錯誤的本身,因爲getFromAPI
在如此多的功能使用,不必不斷地重複嘗試,除了陳述感覺錯,好像它應該在功能getFromAPI
內處理。但是,由於其他functionone
到function_n
都會根據是否出現錯誤返回不同的內容,因此我無法在getFromAPI
之內找到處理該問題的方法,除非有辦法讓getFromAPI
強制它的父函數返回,而沒有顯式調用父函數中的返回值。
失敗了,有沒有更好的實現我想要做的,或者我註定要不斷重複try語句除外?
讓它大聲地失敗並進一步捕捉它......不處理每個函數中的問題 – 2015-04-02 17:47:31
不幸的是,我沒有進一步控制功能。 – echolocation 2015-04-02 17:50:51
「進一步」確實意味着'functionone'..''functionthree'的調用者?或者'getFromAPI'的直接調用者? – kdopen 2015-04-02 17:55:05