如何从另一个 class c# 获取异常
How to get an Exception from another class c#
我有这些class
//Class 1, ViewModel
public async System.Threading.Tasks.Task<JObject> ExecuteSystemObject(string parameters)
{
...
dynamic j = await ExternalProject.ExecuteSomething<MyModel>(parameters);
//How i can catch the error from the another class?
...
}
//Class2, Manager
public async Task<Object> ExecuteSomething<T>() where T : IModel, new()
{
...
WebResponse response = await ExternalProject.ExecuteRequestAsync(PostRequest);
...
}
//Class 3, from a binding Project
public static async Task<WebResponse> ExecuteRequestAsync(WebRequest request)
{
try
{
return await request.GetResponseAsync();
}
catch(WebException e)
{
var resp = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();
dynamic obj = JsonConvert.DeserializeObject(resp);
//I have the message error here
var messageFromServer = obj.error.text;
throw e;
}
}
我只能在最后一个 class 中得到错误,如果我尝试在另一个中得到 WebException,它将对我来说 return null。然后,我如何将该错误传递给主要 class(1º one, ViewModel)?
只要您想要重新抛出异常以便能够保留堆栈跟踪,请始终使用 throw;
。
public async System.Threading.Tasks.Task<JObject> ExecuteSystemObject(string parameters)
{
try
{
dynamic j = await ExternalProject.ExecuteSomething<MyModel>(parameters);
//How i can catch the error from the another class?
...
}
catch(Exception e)
{
//WebException will be caught here
}
}
public async Task<Object> ExecuteSomething<T>() where T : IModel, new()
{
try
{
WebResponse response = await ExternalProject.ExecuteRequestAsync(PostRequest);
}
catch(Exception)
{
throw;
}
}
public static async Task<WebResponse> ExecuteRequestAsync(WebRequest request)
{
try
{
//return await request.GetResponseAsync();
throw new WebException("Test error message");
}
catch(WebException e)
{
throw;
}
}
编辑:只是一条经验法则,只有在您需要处理异常时才捕获异常。如果您正在捕获异常只是为了记录它。不要这样做。
我有这些class
//Class 1, ViewModel
public async System.Threading.Tasks.Task<JObject> ExecuteSystemObject(string parameters)
{
...
dynamic j = await ExternalProject.ExecuteSomething<MyModel>(parameters);
//How i can catch the error from the another class?
...
}
//Class2, Manager
public async Task<Object> ExecuteSomething<T>() where T : IModel, new()
{
...
WebResponse response = await ExternalProject.ExecuteRequestAsync(PostRequest);
...
}
//Class 3, from a binding Project
public static async Task<WebResponse> ExecuteRequestAsync(WebRequest request)
{
try
{
return await request.GetResponseAsync();
}
catch(WebException e)
{
var resp = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();
dynamic obj = JsonConvert.DeserializeObject(resp);
//I have the message error here
var messageFromServer = obj.error.text;
throw e;
}
}
我只能在最后一个 class 中得到错误,如果我尝试在另一个中得到 WebException,它将对我来说 return null。然后,我如何将该错误传递给主要 class(1º one, ViewModel)?
只要您想要重新抛出异常以便能够保留堆栈跟踪,请始终使用 throw;
。
public async System.Threading.Tasks.Task<JObject> ExecuteSystemObject(string parameters)
{
try
{
dynamic j = await ExternalProject.ExecuteSomething<MyModel>(parameters);
//How i can catch the error from the another class?
...
}
catch(Exception e)
{
//WebException will be caught here
}
}
public async Task<Object> ExecuteSomething<T>() where T : IModel, new()
{
try
{
WebResponse response = await ExternalProject.ExecuteRequestAsync(PostRequest);
}
catch(Exception)
{
throw;
}
}
public static async Task<WebResponse> ExecuteRequestAsync(WebRequest request)
{
try
{
//return await request.GetResponseAsync();
throw new WebException("Test error message");
}
catch(WebException e)
{
throw;
}
}
编辑:只是一条经验法则,只有在您需要处理异常时才捕获异常。如果您正在捕获异常只是为了记录它。不要这样做。