五陵年少金市东,银鞍白马渡春风。这篇文章主要讲述exception disappear when forgot to await an async method相关的知识,希望能为你提供帮助。
https://github.com/aspnet/AspNetWebStack/issues/235
https://stackoverflow.com/questions/5383310/catch-an-exception-thrown-by-an-async-void-method
如果异常发生在1个async方法中,而调用这个方法的地方,没有做await。那么异常就会消失。
It‘s somewhat weird to read but yes, the exception will bubble up to the calling code - but only if you await
or Wait()
the call to Foo
.
public async void DoFoo()
{
try
{
await Foo();
}
catch (ProtocolException ex)
{
// The exception will be caught because you‘ve awaited
// the call in an async method.
}
}//or//public void DoFoo()
{
try
{
Foo().Wait();
}
catch (ProtocolException ex)
{
/* The exception will be caught because you‘ve
waited for the completion of the call. */
}
}
Async void methods have different error-handling semantics. When an exception is thrown out of an async Task or async Task method, that exception is captured and placed on the Task object. With async void methods, there is no Task object, so any exceptions thrown out of an async void method will be raised directly on the SynchronizationContext that was active when the async void method started. - https://msdn.microsoft.com/en-us/magazine/jj991977.aspxNote that using Wait() may cause your application to block, if .Net decides to execute your method synchronously.
This explanation http://www.interact-sw.co.uk/iangblog/2010/11/01/csharp5-async-exceptions is pretty good - it discusses the steps the compiler takes to achieve this magic.
Async/Await - Best Practices in Asynchronous Programming
【exception disappear when forgot to await an async method】
推荐阅读
- 微信域名防屏蔽技术,APP推广微信域名怎么避免防红,如何防拦截()
- Android创建列表并为列表添加数据
- kibana设置mapping
- GT性能测试Android版使用说明
- cephapplication not enabled 的解决方法--2019_9
- imx6q android 添加开机启动脚本
- Vue2.5开发去哪儿网App 城市列表开发之兄弟组件间联动及列表性能优化
- NET Core 实战 Dapper 扩展数据访问
- springboot 通用Mapper使用