C
C#4mo 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
wasabi4mo 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
ShevaKaduOP4mo ago
that's what the code deoes httpclient
wasabi
wasabi4mo ago
And so why are you disposing it?
ShevaKadu
ShevaKaduOP4mo 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
wasabi4mo ago
And where do you create it? Because it's not created in this method.
ShevaKadu
ShevaKaduOP4mo 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
Patrick4mo ago
you need to await your code properly
ShevaKadu
ShevaKaduOP4mo ago
how?
Patrick
Patrick4mo ago
by using await
ShevaKadu
ShevaKaduOP4mo ago
in post:
If I change it to response = await client.GetAsync(expectedURL); it throws a TaskCancelledException , neither of which get caught.
Patrick
Patrick4mo 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
ShevaKaduOP4mo ago
updated the post again that is the code that I have, I reverted it and it still doesn't work
Patrick
Patrick4mo 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
ShevaKaduOP4mo ago
and then a "catch" without that below
Patrick
Patrick4mo ago
you're handling both exception branches in the same way as well.
ShevaKadu
ShevaKaduOP4mo ago
shouldn't it trigger?
Patrick
Patrick4mo ago
yes, it will "trigger".
ShevaKadu
ShevaKaduOP4mo ago
but the app still crashes due to it???
Patrick
Patrick4mo ago
I don't know what "it" is, if your app is crashing, it's not due to that snippet
ShevaKadu
ShevaKaduOP4mo ago
it points to that line
Patrick
Patrick4mo ago
again, I don't know what "it" is
ShevaKadu
ShevaKaduOP4mo ago
taskcancelledexception - task cancelled due to httpclient reaching timeout that is what it said
Patrick
Patrick4mo ago
if you use it one more time, I'm going to leave this thread
ShevaKadu
ShevaKaduOP4mo ago
the log
Patrick
Patrick4mo ago
how does a log point to that line?
ShevaKadu
ShevaKaduOP4mo 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
Patrick4mo 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
ShevaKaduOP4mo ago
why is it not breaking on other exceptions in try/catch blocks? okay, let me try okay, maybe that
Patrick
Patrick4mo 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
ShevaKaduOP4mo 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