對不起,如果標題有點混亂,我有一個函式可以在我的應用程式啟動時連接到 API:
private async void connectFunction()
{
try
{
result = await myAPICall();
// do a lot of stuff with the data here
}
catch (System.ArgumentNullException) // this is only throne when my api access token has expired
{
RefreshTokens();
connectFunction(); //this is what I'm concerned about
}
}
現在所有這些都很好用,但我不知道接下來要做什么,我的訪問令牌已更新,現在我需要再次呼叫相同的函式,但我擔心如果我在 catch 塊中呼叫它出錯了,我的應用程式陷入回圈,有什么建議嗎?
uj5u.com熱心網友回復:
如果您只想重試一次,則不需要回圈(或遞回):
private async void connectFunction()
{
MyResultType result;
try
{
result = await myAPICall();
}
catch (System.ArgumentNullException) // this is only throne when my api access token has expired
{
RefreshTokens();
// If this fails, the exception is not caught here, which
// is a good thing. We know that the token can't be the cause
// because we just refreshed it, so we *want* this error to
// bubble up to our generic UI exception handler.
result = await myAPICall();
}
// do a lot of stuff with the data here
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/382071.html
上一篇:如何在firefox中設定browser.helperApps.neverAsk.saveToDisk以避免使用Selenium匯出Protonmail電子郵件時出現下載彈出視窗
下一篇:將圖片從上到下移動
