C
C#3mo ago
ShevaKadu

✅ HTTPClient's GetAsync errors and doesn't get caught

In this code
c#
private async Task<CheckResult> TestHTTP()
{
...
// above code creates httpclient as client
try
{
(!) response = await client.GetAsync(expectedURL);
}
catch (TaskCanceledException ex) when (ex.InnerException is TimeoutException)
{
client.Dispose();
handler.Dispose();
return CheckResult.TimedOut;
}
catch
{
client.Dispose();
handler.Dispose();
return CheckResult.TimedOut;
}
...
}
c#
private async Task<CheckResult> TestHTTP()
{
...
// above code creates httpclient as client
try
{
(!) response = await client.GetAsync(expectedURL);
}
catch (TaskCanceledException ex) when (ex.InnerException is TimeoutException)
{
client.Dispose();
handler.Dispose();
return CheckResult.TimedOut;
}
catch
{
client.Dispose();
handler.Dispose();
return CheckResult.TimedOut;
}
...
}
client is a Httpclient which is supposed to timeout after a delay. To test that, I tried getting it to access a URL that doesn't work. client.GetAsync( ).Result throws a TaskCancelledException , which doesn't get caught Adding:
This function is supposed to be used in multiple threads. Maybe having the thread error out and shutdown serve as a "catch" block of sorts can work? However that is not optimal
How fix???:kekw:
30 Replies
wasabi
wasabi3mo ago
Well, you obviously have to await it, or the code just goes on even though the request isn't done and hasn't errored yet. And then you just need to catch it correctly. What is 'client'?
ShevaKadu
ShevaKadu3mo ago
that's what the code deoes httpclient
wasabi
wasabi3mo ago
And so why are you disposing it?
ShevaKadu
ShevaKadu3mo ago
the code says that the use case that I have for them requires me to create mutliple, so to prevent the problems with doing that, I dispose of them
wasabi
wasabi3mo ago
And where do you create it? Because it's not created in this method.
ShevaKadu
ShevaKadu3mo ago
If I change it to response = await client.GetAsync(expectedURL); it throws a TaskCancelledException , neither of which get caught.
it does, I snipped that part wah @Patrick
Patrick
Patrick3mo ago
you need to await your code properly
ShevaKadu
ShevaKadu3mo ago
how?
Patrick
Patrick3mo ago
by using await
ShevaKadu
ShevaKadu3mo ago
in post:
If I change it to response = await client.GetAsync(expectedURL); it throws a TaskCancelledException , neither of which get caught.
Patrick
Patrick3mo ago
i dont care to guess what the code looks like now many people tell me they've done many things this is user error, so you need to post the actual code that you want help with.
ShevaKadu
ShevaKadu3mo ago
updated the post again that is the code that I have, I reverted it and it still doesn't work
Patrick
Patrick3mo ago
it works, just not the way you want it to 🙂
catch (TaskCanceledException ex) when (ex.InnerException is TimeoutException)
catch (TaskCanceledException ex) when (ex.InnerException is TimeoutException)
there is a condition here, when the inner exception is of type TimeoutException
ShevaKadu
ShevaKadu3mo ago
and then a "catch" without that below
Patrick
Patrick3mo ago
you're handling both exception branches in the same way as well.
ShevaKadu
ShevaKadu3mo ago
shouldn't it trigger?
Patrick
Patrick3mo ago
yes, it will "trigger".
ShevaKadu
ShevaKadu3mo ago
but the app still crashes due to it???
Patrick
Patrick3mo ago
I don't know what "it" is, if your app is crashing, it's not due to that snippet
ShevaKadu
ShevaKadu3mo ago
it points to that line
Patrick
Patrick3mo ago
again, I don't know what "it" is
ShevaKadu
ShevaKadu3mo ago
taskcancelledexception - task cancelled due to httpclient reaching timeout that is what it said
Patrick
Patrick3mo ago
if you use it one more time, I'm going to leave this thread
ShevaKadu
ShevaKadu3mo ago
the log
Patrick
Patrick3mo ago
how does a log point to that line?
ShevaKadu
ShevaKadu3mo ago
I used that code, ran the app in debug (button with filled in green arrow), pressed the button that ran that function, the app crashed, visual studio lights up that line and shows taskcancelledexception
Patrick
Patrick3mo ago
That's not a log then, is it? That's the debugger breaking on an exception. That doesn't mean your app has crashed either. It can mean you're breaking on all exceptions, which means you can just press F5/continue
ShevaKadu
ShevaKadu3mo ago
why is it not breaking on other exceptions in try/catch blocks? okay, let me try okay, maybe that
Patrick
Patrick3mo ago
I don't know. You're expecting a stranger to have remote answers to an environment they are not privy to. It is a guess.
ShevaKadu
ShevaKadu3mo ago
you're right, that is what happened it did catch the error, I changedthe settings to break at all exceptions thanks! the error gets caught properly
Want results from more Discord servers?
Add your server