Results 1 to 3 of 3

Thread: C# keyword enhancement: try / catch / retry / finally

  1. #1

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    C# keyword enhancement: try / catch / retry / finally

    Looking for feedback on this idea I had. I am mostly using C# so I am just thinking about this language but probably this is a .NET framework topic.

    Over the years, I have had to implement a clunky-ugly second try catch inside of the catch block in order to perform a "retry" in case of an error.

    Since I feel that it is not uncommon for us internet software engineers to need to "retry" an operation, should Microsoft include a new keyword, retry to the language?

    I would imagine it would look something like this:
    C# Code:
    1. try
    2. {
    3.   // make a call, log the inputs
    4. }
    5. catch (Exception e)
    6. {
    7.   // oops, log the error
    8. }
    9. retry ()
    10. {
    11.   // since we pass in no value, retry this immediately,
    12.   // but only if we hit the previous catch
    13.   // make the same call, log the inputs
    14. }
    15. catch (Exception e)
    16. {
    17.   // I think we still need a catch, log the error
    18. }
    19. retry (1000)
    20. {
    21.   // since we pass in a value, wait 1,000 ms and then try again,
    22.   // but only if we hit the previous catch
    23.   // make the same call, log the inputs
    24. }
    25. catch (Exception e)
    26. {
    27.   // I think we still need a catch, log the error
    28. }
    29. finally
    30. {
    31.   // cleanup and log event
    32. }

    As you can see I am big on event logging lol. So the idea is, once you succeed in the try, or the first retry, you skip down to the finally. In the example above, the developer wants 2 retries after the first try. I believe you could have as many as you want, as per the discretion of the developer, during coding time, before compile-time.

    I am posting this here because I have always gotten such awesome feedback from this community over the years.

    P.S. I decided not to ask for a wait keyword, because I think it's more readable to pass in a value to the retry to signify a wait time. Why milliseconds? I dunno, I kinda like milliseconds.

    Thanks,

    Dave
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: C# keyword enhancement: try / catch / retry / finally

    I like the idea, but there's already several idiosyncratic patterns for this kind of retry logic?

    Code:
    while (!success && !exitCondition) {
        try {
            ShakyAlgorithm();
            success = true;
        } catch (Exception) {
            // update exitCondition
        }
    }
    The team seems to resist new language features if there's a low-friction solution.

  3. #3

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: C# keyword enhancement: try / catch / retry / finally

    Yes, as I originally stated. Also, your "frictionless" solution does not address waiting, and performing alternate retries that are designed to be polite. Your solution, in a cloud-based, high volume environment, is quite rude, and could kill a system that is simply experiencing momentary difficulty. Here is a more specific example than my original generic example. Notice the politeness factor:

    C# Code:
    1. try
    2. {
    3.   // send email to SMTP server 1
    4. }
    5. catch (Exception e)
    6. {
    7.   // log the error
    8. }
    9. retry (1000)
    10. {
    11.   // send again to same SMTP server 1 after waiting
    12.   // log specific event
    13. }
    14. catch (Exception e)
    15. {
    16.   // log the error specific to retry
    17. }
    18. retry
    19. {
    20.   // send email to SMTP server 2
    21.   // log specific event
    22. }
    23. catch (Exception e)
    24. {
    25.   // log the error specific to retry
    26. }
    27. finally
    28. {
    29.   // cleanup and log event
    30. }
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width