✅ HTTPClient's GetAsync errors and doesn't get caught
In this code
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 optimalHow fix???:kekw:
30 Replies
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'?
that's what the code deoes
httpclient
And so why are you disposing it?
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
And where do you create it?
Because it's not created in this method.
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
you need to await your code properly
how?
by using
await
in post:
If I change it to response = await client.GetAsync(expectedURL); it throws a TaskCancelledException , neither of which get caught.
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.
updated the post again
that is the code that I have, I reverted it
and it still doesn't work
it works, just not the way you want it to 🙂
there is a condition here,
when the inner exception is of type TimeoutException
and then a "catch" without that below
you're handling both exception branches in the same way as well.
shouldn't it trigger?
yes, it will "trigger".
but the app still crashes due to it???
I don't know what "it" is, if your app is crashing, it's not due to that snippet
it points to that line
again, I don't know what "it" is
taskcancelledexception - task cancelled due to httpclient reaching timeout
that is what it said
if you use
it
one more time, I'm going to leave this threadthe log
how does a log point to that line?
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
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
why is it not breaking on other exceptions in try/catch blocks?
okay, let me try
okay, maybe that
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.
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