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.aspx
Note 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的更多相关文章
- await and async
Most people have already heard about the new “async” and “await” functionality coming in Visual Stud ...
- .Net 多线程 异步编程 Await、Async和Task
await和async简介 await和async是在C#5中引入,并且在.NetFramewor4.5以及.NetCore中进行了支持.主要是解决性能瓶颈,并且增强系统的响应能力. msdn关于 ...
- 图解 Await 和 Async
原文链接:Await and Async Explained with Diagrams and Examples 文章目录 简介 Promise 问题:组合 Promise Async 函数 Awa ...
- 小心C# 5.0 中的await and async模式造成的死锁
平时在使用C# 5.0中的await and async关键字的时候总是没注意,直到今天在调试一个ASP.NET项目时,发现在调用一个声明为async的方法后,程序老是莫名其妙的被卡住,就算声明为as ...
- await和async关键字来写异步程序
await和async关键字出现于.Net5.0,方便写异步程序. 例子: public class MyClass { public MyClass() { DisplayValue(); //这里 ...
- C#同步,异步的理解,包括5.0中await和async(学习笔记)
之前在工作中一直用的是同步线程,就是先进入画面的load事件,然后在里面进行数据库调用的处理.后面又遇到了公司软件中一些比较古老的代码,一开始在那块古老代码中增加机能的时候,我想用到数据库的数据给画面 ...
- 【笔记】记一次.net语法await和async的异步编程实验与笔记。
1.实践代码全记录: using System; using System.Collections.Generic; using System.Diagnostics; using System.Li ...
- await和async更多的理解
最近有不少网友提起await和async,呵呵,C# 5引进的语法糖. 这个语法糖还真不好吃,能绕倒一堆初学的朋友,在网上也有很多网友关于这块知识点的争论,有对有错,今天在这里把这个误区好好讲讲. 在 ...
- C#语法——await与async的正确打开方式
C#5.0推出了新语法,await与async,但相信大家还是很少使用它们.关于await与async有很多文章讲解,但有没有这样一种感觉,你看完后,总感觉这东西很不错,但用的时候,总是想不起来,或者 ...
随机推荐
- 如何使用腾讯云开发一款 AR 应用介绍
版权声明:本文由张亚舒原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/117 来源:腾云阁 https://www.qclo ...
- CVE-2018-2628 weblogic WLS反序列化漏洞--RCE学习笔记
weblogic WLS 反序列化漏洞学习 鸣谢 感谢POC和分析文档的作者-绿盟大佬=>liaoxinxi:感谢群内各位大佬及时传播了分析文档,我才有幸能看到. 漏洞简介 漏洞威胁:RCE-- ...
- IOS中使用轻量级数据库
IOS中使用轻量级数据库 目录 概述 IOS中的轻量级数据库 sqlite的方法 数据库的实用操作 第三方类库 FMDatabase 概述 IOS中的轻量级数据库 sqlite的方法 sqlite3 ...
- Gallery 里面怎么设置ImageView的OnClick事件
Gallery g=this.findViewById(R.id.gallery); g.setOnItemClickListener(new OnItemClickListener(){ @Over ...
- 微信小程序 --- 组件
view组件:小程序使用最多的组件.可以理解为 div: Text组件:用于展示文字.可以理解为 p span 标签.(除了Text组件以外的其他组件都无法被长按选中) Text组件无法解析HTML, ...
- vector容器建图
#pragma comment(linker, "/STACK:1024000000,1024000000") #include"stdio.h" #inclu ...
- Java多线程(4)----线程的四种状态
与人有生老病死一样,线程也同样要经历开始(等待).运行.挂起和停止四种不同的状态.这四种状态都可以通过Thread类中的方法进行控制.下面给出了Thread类中和这四种状态相关的方法. 1 // 开始 ...
- 从零打造在线网盘系统之Struts2框架起步
欢迎浏览Java工程师SSH教程从零打造在线网盘系统系列教程,本系列教程将会使用SSH(Struts2+Spring+Hibernate)打造一个在线网盘系统,本系列教程是从零开始,所以会详细以及着重 ...
- centos7yum下载安装报:软件包与预期下载的不符。建议:运行 yum --enablerepo=updates clean metadata尝试其他镜像
centos7yum下载安装报:软件包与预期下载的不符.建议:运行 yum --enablerepo=updates clean metadata尝试其他镜像. rm -f /var/lib/rpm/ ...
- win10配置的静态/动态IP和 DNS的方法
1.配置静态IP和DNS netsh interface ip set address name="以太网" source=static addr=192.168.9.145 ma ...