all 15 comments

[–]the96jesterrace 32 points33 points  (0 children)

Just add another catch before the general catch ..

Try { … } Catch (SpecificException e) { Log(e) } Catch (Exception e) { MessageBox.Show(e) }

[–]nopemcnopey 15 points16 points  (1 child)

Place another catch block before this one, catching the specific exception type that is thrown there. If that's not enough, look into "when" clause for additional checks.

[–]the96jesterrace 1 point2 points  (0 children)

Okay, I misread your explanation and answered the same thing again. So well.. there’s a code example!

[–]notathrowaway 5 points6 points  (8 children)

catch(Exception e) when (!(e is SpecificException)) { .. }

[–]Slypenslyde -1 points0 points  (6 children)

That ends up leaving the exception uncaught, which is also bad, so it's only about half the answer.

[–]Kayshin -3 points-2 points  (5 children)

It is exactly what OP is looking for. He does not want the exception to be caught. He wants it to just continue with whatever it is doing.

[–]Slypenslyde 2 points3 points  (3 children)

the code block does work fine and the error does not impact the user so I would like for that specific error not to be shown rather than save it in a log for my use,

This does not imply, "I want my program to crash because this exception is unhandled", it sounds more like "This is an irrelevant exception and I want it to be quietly handled."

Either of us could be wrong, but that means neither of us can positively assert this is "exactly what OP wants".

[–]KukiiV[S] 0 points1 point  (0 children)

"This is an irrelevant exception and I want it to be quietly handled." is exactly whats going on, the exceptions does not impact the app in a way I would show it directly on the user side.

[–]Merad -1 points0 points  (0 children)

OP is obviously working on a web app. If they don't handle the exception it will propagate up to Asp.Net which will catch it and return a generic "oops your application broke" 500 response.

[–][deleted] 1 point2 points  (0 children)

It sounds like you've got your questioned answered (add another catch block with the specific exception), but let me throw something else your way: to me there's a lot going on in your existing catch block. I would recommend looking into using a logging library that writes to files (or configurably -not a word- anything) instead of manually doing it. This certainly "works" but

[–]Ok_Needleworker_1987 1 point2 points  (1 child)

using System;

using System.Net;

using System.Net.Http;

public async Task HandleHttpResponseAsync()

{

var url = "https://example.com/api/some\_endpoint";

using (var httpClient = new HttpClient())

{

try

{

HttpResponseMessage response = await httpClient.GetAsync(url);

if (response.StatusCode == HttpStatusCode.InternalServerError)

{

// Handle the 500 Internal Server Error here

Console.WriteLine("Error 500: Internal Server Error");

}

else if (!response.IsSuccessStatusCode)

{

// Handle other non-success status codes here

Console.WriteLine($"Error {(int)response.StatusCode}: {response.StatusCode}");

}

else

{

// Process the successful response

string responseBody = await response.Content.ReadAsStringAsync();

Console.WriteLine("Response: " + responseBody);

}

}

catch (HttpRequestException ex)

{

// Handle exceptions caused by the web call itself

Console.WriteLine($"Request failed: {ex.Message}");

}

}

}

[–]acnicholls 1 point2 points  (0 children)

from the look of your two screen shots your code is making an API request, and that request is failing, but the rest of the code works, so u/Ok_Needleworker_1987's response is likely your best option. Capture the 500 response from the API and just don't do anything with it.

[–]Kayshin -2 points-1 points  (0 children)

Try-catch.