在catch块中获取异常错误代码 [英] get exception error code in catch block

查看:324
本文介绍了在catch块中获取异常错误代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

如何从catch(异常ex)获取错误代码,说我得到错误请求失败.远程服务器返回错误:(401)未经授权."

我只需要错误代码401.

谢谢,
krunal

hi all,

how do i get error code from catch (Exception ex), say i got error "The request failed. The remote server returned an error: (401) Unauthorized."

i want error code 401 only.

thanks,
krunal

推荐答案

您可以尝试捕获WebException.

You can try to catch WebException.

catch (WebException ex)
{
  HttpWebResponse resp = (HttpWebResponse)ex.Response;
  if (resp!= null)
  {
   int httpCode = (int)response.StatusCode;
  }
}



希望能有所帮助.如果是这样,请将其标记为答案/赞.

谢谢
Milind



Hope that helps. if it does, mark it as answer/upvote.

Thanks
Milind




您正在使用哪种身份验证类型以及您正在使用哪种类型的应用程序?

谢谢.
Hi,

what is the authentication type you are using and what type of an application are you working on?

Thanks.


这篇关于在catch块中获取异常错误代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆