Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does GOTO in CATCH block in c# execute FINALLY?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading