c# - 如何从任务中抛出/捕获内部异常?
问题描述
假设我有一个简单的方法:
private void MyMethod()
{
try {
myService.Do();
} catch (MyException ex) {}
}
我有一个使用 的服务,HttpClient
我这样做:
public void Do()
{
var response = client.GetAsync(url).Result; //Alas it's not async till now
}
现在我正在实施 aDelegatingHandler
并覆盖SendAsync
:
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return await base.SendAsync(request, cancellationToken)
.ContinueWith<HttpResponseMessage>(task =>
{
HttpResponseMessage response = task.Result;
if (response.StatusCode == HttpStatusCode.NotFound)
{
throw new MyException();
}
return response;
}).ConfigureAwait(false);
}
一切都按预期工作,直到在MyException
这里抛出 a 。异常冒泡到调用者,但是,异常是AggregrateException
.
有没有办法抛出实际的异常本身,这样就不需要对应用程序范围内的代码进行重大更改?
解决方案
当在任务中引发异常时,使用Result
属性您将获得AggregateException
真正的异常在InnerException
该AggregateException
对象的属性中的位置(您的异常由 包裹AggregateException
)。
要获得真正的异常(未包装的异常),您可以使用GetAwaiter().GetResult()
:
var result = taskThatThrowsException.GetAwaiter().GetResult();
您也可以使用Result
属性,但是您应该为异常处理指定一些条件;
try
{
var result = taskThatThrowsException.Result;
}
catch (AggregateException ex) when (ex.InnerException is MyException myException)
{
// use myException - it is your true unwrapped exception
}
但是你不应该阻塞异步代码- 你应该异步等待 - 使用await
你也会得到你真正的解包异常:
var result = await taskThatThrowsException;
推荐阅读
- c# - 按天迭代 linq 查询结果以检查条件
- symfony - 使用 Webpack Encore ^0.21.0 时的未知“encore_entry_link_tags”函数
- amazon-web-services - 使用 Auto Scaling 组、CloudFormation 和 CodeDeploy 的蓝/绿部署
- javascript - Summernote - 如何在向图像添加链接时禁用“要显示的文本”
- javascript - 用非线性数据计算方程
- python - TypeError: zip 参数 #2 必须支持迭代 - Python 3
- java - 为 MongoDb 访问问题添加凭据
- java - 将arraylist中的数据插入JTable的单元格
- c# - 如何为 Telesharp 设置代理
- c - C, 为什么函数不接受 txt 文件中的二维数组作为参数?