1

I got the following:

public async Task<bool> IsAuthenticatedAsync()
    {
        using (HttpClient client = new HttpClient())
        {
            using (MultipartFormDataContent content = new MultipartFormDataContent())
            {
                content.Add(new StringContent(this.Username, Encoding.UTF8), "username");
                content.Add(new StringContent(this.Password, Encoding.UTF8), "password");
                try
                {
                    HttpResponseMessage message = await client.PostAsync(authenticatedUrl, content);
                    if (message.StatusCode == HttpStatusCode.Accepted)
                        return true;
                    return false;
                }
                catch(HttpRequestException)
                {
                    System.Windows.Forms.MessageBox.Show("Some text.");
                }
            }
        }
        return false;
    }

where authenticatedUrl is some static Uri.

Now assuming that the webserver is not available (or the adress is wrong) await client.PostAsync(authenticatedUrl, content) throws a HttpRequestException, hence the try-catch.

Problem is that the exception is not getting caught. I tried turning off Just My Code but that just adds other exceptions (i.e. SocketException) as suggested here and still doesn't let catch handle the exception.

Why isn't the exception being caught?

Edit

main form(GUI):

public GUI(...)
{
    ...
    CheckLoginState(username, password);
    ...
}

private async void CheckLoginState(string username, string password)
{
    User user = new User(username, password);
    if (user.Username != null && user.Password != null && await user.IsAuthenticatedAsync())
        abmeldenToolStripMenuItem.Enabled = true;
}
Community
  • 1
  • 1
VGD
  • 436
  • 1
  • 5
  • 25
  • 1
    Have you tried to catch `AggregateException` – Ivan Stoev Nov 10 '15 at 15:02
  • Are you sure an `HttpRequestException` is being thrown? Try enabling first chance exceptions – Yuval Itzchakov Nov 10 '15 at 15:06
  • I'm getting a `System.Net.Http.HttpRequestException` (first chance) in the debugger. – VGD Nov 10 '15 at 15:10
  • 1
    @IvanStoev I did, still a `System.Net.Http.HttpRequestException` is thrown which is not caught by the `try-catch`. – VGD Nov 10 '15 at 15:12
  • Can you show the code where you are calling the IsAuthenticatedAsync method? – Praveen Paulose Nov 10 '15 at 15:25
  • @PraveenPaulose see edit. – VGD Nov 10 '15 at 15:32
  • I just ran your code. It does catch the Exception. When you say it is not catching the Exception, what exactly happens? The app crashes? – Praveen Paulose Nov 10 '15 at 15:37
  • I shows me the debugger exception window which you get if you don't catch a exception. – VGD Nov 10 '15 at 15:44
  • 2
    _"shows me the debugger exception window which you get if you don't catch a exception"_ -- unless in the Debug/Exceptions... settings you have told the debugger to always break on exceptions. Given the code and operation you describe, that's the most likely explanation for your concern. If you've checked the settings and are sure they are as you expect, you will need to provide [a good, _minimal_, _complete_ code example](http://stackoverflow.com/help/mcve) that reliably reproduces the problem. There's nothing in the code you've shown that would suggest anything other than expected results. – Peter Duniho Nov 10 '15 at 17:04
  • Does that mean _always break on exception_ does _not_ mean that it only breaks if unhandled but that it also breaks if _handled_ ? – VGD Nov 10 '15 at 17:08

1 Answers1

0

I'm pretty sure the exception is not being caught, because it is wrapped in the task returned from the call to "CheckLoginState"-method, and since you are not awaiting that task, then your exception will never get thrown. From the looks of it, you are calling it from a constructor so I'm going to assume you can't make the "public GUI" into an "public async void GUI".

For the sake of testing, you could try a blocking wait ex:

public GUI(...)
{
    ...
    var task = CheckLoginState(username, password).Wait();
    if(task.IsFaulted && task.Exception != null)
    {
        throw task.Exception
    }
    ...
}

Another way would be to let the async/await-pattern to spread naturally and tie it up to an event (Don't know if you're using wpf or winforms, so I'll assume wpf).

public GUI(...)
{
    this.Loaded += async (obj, eventArgs) => await CheckLoginState(username, password);
    ...
}

Of course, it doesn't have to be the "loaded" event. Point is, that an event can be async, a constructor can't (from what I know anyway).

Shazi
  • 1,490
  • 1
  • 10
  • 22