0

In the try catch block want to get the catch exception code, we know that 404: not found, 400: bad request. In my catch block want to get exception code. How to get exception code in C# catch block.

 try
            {
                await next(context);
            }
            catch (Exception ex)
            {

            }
shamim
  • 6,640
  • 20
  • 85
  • 151

1 Answers1

3

You can just catch the WebException, and then, check the ProtocolError and status code.

}
catch(WebException e) {
    if(e.Status == WebExceptionStatus.ProtocolError) {
        Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
        Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
    }
}
catch(Exception e)
{
//
}
Bruno Caceiro
  • 7,035
  • 1
  • 26
  • 45