Click here to Skip to main content
13,355,018 members (62,770 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 9 Sep 2013


, 9 Sep 2013
Rate this:
Please Sign up or sign in to vote.
A small tip to retry an action, with an easy to use extension.


This is just a short description of my thoughts of handling exceptions and at the same time offering a retry.

Try Repeat 

I created an extension method just to make it convenient to use. The method extends an Action so that you just have to call (Action).TryRepeat(method) with a method that returns a Boolean to indicate if the action should retry or not. 

/// <summary>
/// Tries the specified action.
/// </summary>
/// <param name="action">The action.</param>
/// <param name="failAction">The fail action.</param>
/// <returns></returns>
public static void TryRepeat(this Action action, Func<Exception, bool> failAction)
  bool repeat;
    catch (Exception e)
      if (Debugger.IsAttached) Debugger.Break();
      repeat = failAction(e);
  } while (repeat);

To use the extension method you can do this:

Action a = () => 
   // Put your code here

The evaluation function takes the Exception as input and returns a Boolean value to indicate if the action should be repeated. It could look like this:

public static bool HandleException(Exception e)
  if (e is SomeException)
    MessageBoxResult result = 
      MessageBox.Show("Connection seems lost, reconnect?", "Error",
    return result == MessageBoxResult.Yes;
  return false;

Points of Interest 

I'm not thrilled about my idea, mainly because retrying actions might provoke unforeseen errors.


  • 1.0: My initial thoughts.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Niel M.Thomas
Denmark Denmark
Name: Niel Morgan Thomas
Born: 1970 in Denmark
Dataengineer from Odense Technical University.
More than 20 years in IT-business.
Current employment:
Working with application development in a major Danish company that produce medical equipment.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Mario Z5-Oct-15 0:18
professionalMario Z5-Oct-15 0:18 
GeneralMy vote of 5 Pin
_Nizar9-Sep-13 21:18
member_Nizar9-Sep-13 21:18 
SuggestionSend repeat to action Pin
Anton Minko9-Sep-13 5:06
professionalAnton Minko9-Sep-13 5:06 
QuestionAn interesting idea, and I'd like to see where you could take this Pin
Pete O'Hanlon9-Sep-13 3:52
protectorPete O'Hanlon9-Sep-13 3:52 
AnswerRe: An interesting idea, and I'd like to see where you could take this Pin
_Nizar9-Sep-13 21:18
member_Nizar9-Sep-13 21:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 9 Sep 2013
Article Copyright 2013 by Niel M.Thomas
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid