Home > Back-end >  Does GOTO in CATCH block in c# execute FINALLY?
Does GOTO in CATCH block in c# execute FINALLY?

Time:02-01

Pretty simple question:

int retryAttempts = 3;

retry:
try {
    await getSemaphore();
    throw new Exception();
}
catch {
    if (0 > retryAttempts--) {
        await Task.Delay(5000);
        goto retry;
    }

    return;
}
finally {
    releaseSemaphore();
}

In this example, will the semaphore be released one or three times?

CodePudding user response:

finally will execute every time you leave the catch block. So in your case releaseSemaphore() will be called three times (after each goto).

I also invite you to read the official documentation about try-finally here

  •  Tags:  
  • Related