Sorry if the the title is a little confusing, I have a function that connects to an API when my app starts:
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
}
}
right now all this works great but I don't known what to do next, my access token has been updated and now I need to call the same function again, but I'm concerned that if I call it in the catch block something could go wrong and my app gets stuck in a loop, any advice?
CodePudding user response:
If you just want to retry once, there's no need for a loop (or recursion):
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
}