如何捕捉异常? [英] How to catch exception?
问题描述
我试图调用API,并检查它的反应,但是当过一些错误的值传递停止该程序。我想在请求和响应,但不知道如何在函数中加上例外。
I am trying to call api and check its response, but when ever some wrong value is passed it stops the program. I want to add exception during request and response but not sure how to write in function.
这是我怎么叫我的REST调用
This is how i call my REST call
public dynamic APICalls(JObject ljson, string endpoints, string method)
{
var httpReq = (HttpWebRequest)HttprequestObject(endpoints, method);
using (var streamWriter = new StreamWriter(httpReq.GetRequestStream()))
{
streamWriter.Write(ljson);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpReq.GetResponse();
var result = "";
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
return result;
//return "Success";
//not sure what to return
//here i have to add sql server code to enter into database
}
这是$ C $下要求
public dynamic HttprequestObject(string endpoints, string method)
{
string url = Settings.API_TEST_VALUE + endpoints;
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = method;
return httpWebRequest;
}
和之前的要求,我想捕捉异常后,正确的反应的权利。
And right before request and right after response i want to catch exception.
在这一点上我要赶异常
var httpResponse = (HttpWebResponse)httpReq.GetResponse();
如果有人给我暗示如何捕捉它停止程序之前。
If some one gives me hint how to catch that before it stops program.
有400,401,402错误,如果事情是错的API发送JSON
There are 400, 401,402 errors, if something is wrong API sends json
例如,在创建用户: - 电子邮件ID已经存在
For instance, while creating user :-- Email id already exists
但是,这点停止JSON和终止程序。
But that points stops json and stops program..
使用尝试捕捉它会停止程序,我希望它运行,并希望接收resposne。
Using try catch it will stop program, I want it to run and want to receive resposne.
其实,API将发送错误。 例如,状态会; --- 401未授权 和resposnse将
Actually, API will send error . For instance, status will be ;---401 UNAUTHORIZED and resposnse will be
{ "reason": "Email already registered.", "success": false }
我改变了我的code和
I am changed my code and
HttpWebResponse httpResponse;
try
{
//HttpWebResponse myHttpWebResponse = (HttpWebResponse)httpReq.GetResponse();
httpResponse = (HttpWebResponse)httpReq.GetResponse();
//myHttpWebResponse.Close();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
}
catch (WebException e)
{
Console.WriteLine("This program is expected to throw WebException on successful run." +
"\n\nException Message :" + e.Message);
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)
{
Console.WriteLine(e.Message);
}
return result;
//return "Success";
//not sure what to return
//here i have to add sql server code to enter into database
}
这是新的code,但我很老JSON作为返回值,所以我可以显示特定的错误。 对于下面的Json我应该怎么写呢?
This is the new code, but I m not getting Json as return value, So i can show specific error. For below Json what should I write?
{理由:电子邮件已经注册,成功:假}
{ "reason": "Email already registered.", "success": false }
请我为新的C#,如果事情是不明确,请修改或问的问题? 谢谢
please I m new to c# and if something is not clear please modify or ask question? thank you
推荐答案
您正在寻找所谓的的 的try-catch
语句:
What you're looking for is called a try-catch
statement:
try
{
var httpResponse = (HttpWebResponse)httpReq.GetResponse();
}
catch (WebException e)
{
// Here is where you handle the exception.
}
使用 WebException
如抓类型
语句的意思是特定类型的唯一例外的会被抓住。
Using WebException
as the type in the catch
statement means only exceptions of that particular type will be caught.
在的情况下发生异常时,电子
变量将包含异常的详细信息,如从内这三个异常,内部异常封装的方法传递消息。
In case an exception occurs, the e
variable will contain exception details, such as a message passed from the method which three the exception and any inner exceptions encapsulated inside.
这篇关于如何捕捉异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!