ASP.NET MVC:获取停止在重新抛出的 catch 块中的未处理异常
ASP.NET MVC: Getting unhandled exception that stops in a catch block of a rethrow
我在 try/catch-rethrow 代码块中看到一些奇怪的行为。
我有一个实例化 ClientGet 的 class 并从中获取结果值的方法 class。
public Task<T> Get<T>(ClientRequest<T> Request)
{
try
{
return new ClientGet<T>(UpdateRequest(Request)).Result;
}
catch (Exception)
{
throw;
}
}
ClientGet class 看起来像这样:
public class ClientGet<T> : IClientAction<T>
{
public Task<T> Result { get; set; }
public ClientGet(ClientRequest<T> request)
{
try
{
Result = GetResult(request);
}
catch (Exception)
{
throw;
}
}
private async Task<T> GetResult(ClientRequest<T> request)
{
if (string.IsNullOrEmpty(request.FullPath))
{
throw new ApplicationException("You must specify a path in your request.");
}
try
{
using (HttpClient client = new HttpClient())
{
if (!string.IsNullOrEmpty(request.Settings.Token)) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", request.Settings.Token); }
var response = await client.GetAsync(request.FullPath);
if (response.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync());
}
throw new HttpRequestException((int)response.StatusCode + ": " + response.ReasonPhrase);
}
}
catch (Exception)
{
throw;
}
}
}
如果 response.IsSuccessStatusCode 为假,出于某种原因,代码会在我的 GetResult 方法的捕获处停止,并将其作为 ASP.NET 未处理的异常错误发布到我的抛出;线。我有所有的重新抛出,以便可以将错误传递回应用程序更高级别的原始调用者。
这是我收到的确切消息:
401: Unauthorized
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.Http.HttpRequestException: 401: Unauthorized
Source Error:
Line 45: catch (Exception)
Line 46: {
Line 47: throw;
Line 48: }
Line 49: }
不确定您希望发生什么。听起来代码正在做它应该做的事情。如果不满足条件,它会继续到您抛出 HttpRequestException
的新的下一行,因为这全部包含在 try-catch 中,该异常被捕获,然后重新抛出。然而,这是第一次允许异常冒泡,所以这是 Visual Studio 将指示异常起源的地方。最终会被调用方法的try-catch捕获,再抛到那里后最终提升为未捕获状态
但是,您在这里违反了使用 try-catch 的基本规则:仅当您打算实际处理时才捕获异常。如果您要做的只是将它扔到 catch 块中,那么整个 try-catch 就毫无意义。默认行为是允许它在堆栈中冒泡;你不必明确地抛出它来完成它。
我在 try/catch-rethrow 代码块中看到一些奇怪的行为。
我有一个实例化 ClientGet 的 class 并从中获取结果值的方法 class。
public Task<T> Get<T>(ClientRequest<T> Request)
{
try
{
return new ClientGet<T>(UpdateRequest(Request)).Result;
}
catch (Exception)
{
throw;
}
}
ClientGet class 看起来像这样:
public class ClientGet<T> : IClientAction<T>
{
public Task<T> Result { get; set; }
public ClientGet(ClientRequest<T> request)
{
try
{
Result = GetResult(request);
}
catch (Exception)
{
throw;
}
}
private async Task<T> GetResult(ClientRequest<T> request)
{
if (string.IsNullOrEmpty(request.FullPath))
{
throw new ApplicationException("You must specify a path in your request.");
}
try
{
using (HttpClient client = new HttpClient())
{
if (!string.IsNullOrEmpty(request.Settings.Token)) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", request.Settings.Token); }
var response = await client.GetAsync(request.FullPath);
if (response.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync());
}
throw new HttpRequestException((int)response.StatusCode + ": " + response.ReasonPhrase);
}
}
catch (Exception)
{
throw;
}
}
}
如果 response.IsSuccessStatusCode 为假,出于某种原因,代码会在我的 GetResult 方法的捕获处停止,并将其作为 ASP.NET 未处理的异常错误发布到我的抛出;线。我有所有的重新抛出,以便可以将错误传递回应用程序更高级别的原始调用者。
这是我收到的确切消息:
401: Unauthorized Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.Http.HttpRequestException: 401: Unauthorized
Source Error:
Line 45: catch (Exception)
Line 46: {
Line 47: throw;
Line 48: }
Line 49: }
不确定您希望发生什么。听起来代码正在做它应该做的事情。如果不满足条件,它会继续到您抛出 HttpRequestException
的新的下一行,因为这全部包含在 try-catch 中,该异常被捕获,然后重新抛出。然而,这是第一次允许异常冒泡,所以这是 Visual Studio 将指示异常起源的地方。最终会被调用方法的try-catch捕获,再抛到那里后最终提升为未捕获状态
但是,您在这里违反了使用 try-catch 的基本规则:仅当您打算实际处理时才捕获异常。如果您要做的只是将它扔到 catch 块中,那么整个 try-catch 就毫无意义。默认行为是允许它在堆栈中冒泡;你不必明确地抛出它来完成它。