/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/fundamentals/networking/snippets/httpclient/Program.ThrowHttpException.cs
24 строки
765 B
David Pine
Add code examples and update content for HTTP error handling (#31780)
14 окт 2022, 23:18
Не верифицирован
14 окт 2022, 23:18
2a44b4b
Код
Авторство
О чём код?
static partial class Program { static async Task ThrowHttpRequestExceptionAsync(HttpClient httpClient) { // <throw> try { using var response = await httpClient.GetAsync( "https://localhost:5001/doesNotExists"); // Throw for anything higher than 400. if (response is { StatusCode: >= HttpStatusCode.BadRequest }) { throw new HttpRequestException( "Something went wrong", inner: null, response.StatusCode); } } catch (HttpRequestException ex) when (ex is { StatusCode: HttpStatusCode.NotFound }) { Console.WriteLine($"Not found: {ex.Message}"); } // </throw> } }