I’m new in .Net development, and I’m looking for some advices.
I’ve some piece of code which throw some exceptions. These exception are thrown if my database is down for example. I want to let users have the capability to retry the executed code if failed.
Here my initial code :
executeRequest();
What I’ve thinked in C-style (but working anyway in C#):
do
{
try
{
executeRequest();
return;
}
catch (Exception exception)
{
ErrorMessagesUtils.ShowTaskErrorMessage(exception);
}
} while (MessageBox.Show("Failed : Retry ?", "Error",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes);
It’s working but it’s really annoying to reproduce it with other methods. I want to use with executeRequest but not only.
If I was coding in C, I’ll use #define ASK_USER_TO_RETRY(funcname) ...code.... But in C#, I’ve not found a way to reuse a piece of code in this manner. Some thoughts?
You can create method, which accepts delegate (method to be executed):
Usage: