3

I am using polly policy for retry attempts in the following way:

results = await Policy
                .Handle<WebException>()
                .WaitAndRetryAsync
                (
                    retryCount: 5,
                    sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))
                )
                .ExecuteAsync(async () => await task.Invoke());

I am using the AsyncErrorHandler to handle all the web exceptions:

public static class AsyncErrorHandler
{
    public static void HandleException(Exception ex)
    {
        Debug.WriteLine(ex.Message);
    }
}

However some of the expections I would like to throw up to the GUI. With this code in place how can I prevent handling of a specific exception and instead throw it to the GUI?

[UPDATE] If I throw a specific exception inside the HandleException function I receive an Unhandled Error Message dialog in Visual Studio.

doorman
  • 15,707
  • 22
  • 80
  • 145

1 Answers1

2

Implement it differently to only throw errors on the ones you want displayed to a user, then catch those you want to throw and do what you want with their contents (either show to user or not).

try
{
      results = await Policy
            .Handle<WebException>()
            .WaitAndRetryAsync
            (
                retryCount: 5,
                sleepDurationProvider: retryAttempt =>  TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))
            )
            .ExecuteAsync(async () => await task.Invoke());
}

catch (ExceptionToThrowToUser ex)
{
    MessageBox.Show(ex.Message);
}


public static class AsyncErrorHandler
{
    public static void HandleException(Exception ex)
    {
        if (ex is ExceptionToThrowToUser)
        {
           throw;               
        }
        else
            Debug.WriteLine(ex.Message);
    }
}

Editted for update.

For help handling errors: Best practices for catching and re-throwing .NET exceptions

Community
  • 1
  • 1
Joshua Smith
  • 134
  • 10
  • The code you posted, though, doesn't use the AsyncErrorHandler that's cited in the question. – Sam Hanley Dec 01 '15 at 21:03
  • The HandleException is always called when I receive an exception. If I throw the error inside the AsyncErrorHandler class a dialog pops up in Visual Studio showing the error instead of populating the exception up to the GUI. – doorman Dec 01 '15 at 21:14
  • How are you throwing? Are you just showing the message or literally throw ex;? That's why I personally just throw the message in a MessageBox for the user to see if I want them to. – Joshua Smith Dec 01 '15 at 22:51
  • Updated answer. Yeah, you still have to handle the error in your code after it is thrown! – Joshua Smith Dec 01 '15 at 22:56
  • Yes, I am just throwing the ex because I want to handle it specially in the GUI code, clearing some variables. – doorman Dec 02 '15 at 08:52
  • Well, this should throw it and catch it where you have the catch statement. – Joshua Smith Dec 02 '15 at 20:14
  • The problem seems to be when I try to throw it within the AsyncErrorHandler class which catches all the exceptions. It doesn´t propogate to the GUI from there resulting in a Unhandled Error Message. I will check with the author. – doorman Dec 03 '15 at 10:25
  • Yeah! You have to handle it there, glad it helped! – Joshua Smith Dec 03 '15 at 20:26