Await in Catch and Finally

简介:

This is just a brief note to publicize a coming improvement to the asynclanguage support.

Visual Studio “14” is currently in CTP, and is available for download. One of the primary advantages of this release is the new Roslyn-based compilers.

Note that the “14” in the name is the version number, not the year of release. In other words, the CTP is for “Visual Studio 14”, not“Visual Studio 2014”. If I had to guess, I would say that this CTP will probably become “Visual Studio 2015”.

With the new compilers, changes to the C# language (e.g., async/await) are easier than they used to be. One improvement that is coming is the use of await in catch and finally blocks. This enables your error-handling/cleanup code to be asynchronous without awkward code mangling.

For example, let’s say that you want to (asynchronously) log an exception in one of your async methods. The natural way to write this is:

1
2
3
4
5
6
7
try {
   await OperationThatMayThrowAsync();
   }
   catch  (ExpectedException ex)
   {  
   await MyLogger.LogAsync(ex);
   }

And this natural code works fine in Visual Studio “14”. However, the currently-released Visual Studio 2013 does not support await in a catch, so you would have to keep some kind of “error flag” and move the actual error handling logic outside the catch block:

ExpectedException exception = null;
try
{
    await OperationThatMayThrowAsync();
  }
  catch (ExpectedException ex)
  {
  exception = ex;
  }
  
  if (exception != null)
     await MyLogger.LogAsync(exception);

This is only a simple example; in real-world code, this can get ugly rather quickly!

Fortunately, it looks like the next version of Visual Studio will fix this by allowing await within catch and finally blocks. I’ve tested this out with the Visual Studio “14” CTP, and it does work!

from:http://blog.stephencleary.com/2014/06/await-in-catch-and-finally.html
















本文转自cnn23711151CTO博客,原文链接:http://blog.51cto.com/cnn237111/1549821 ,如需转载请自行联系原作者


相关文章
|
消息中间件 弹性计算 Kubernetes
RabbitMQ与容器化技术的集成实践
【8月更文第28天】RabbitMQ 是一个开源消息代理和队列服务器,用于在分布式系统中存储、转发消息。随着微服务架构的普及,容器化技术(如 Docker 和 Kubernetes)成为了部署和管理应用程序的标准方式。本文将探讨如何使用 Docker 和 Kubernetes 在生产环境中部署和管理 RabbitMQ 服务,同时保证高可用性和弹性伸缩能力。
337 3
|
NoSQL Java Spring
教程:Spring Boot与ETCD键值存储的整合
教程:Spring Boot与ETCD键值存储的整合
|
前端开发 Python
Python float(input())的用法,web中的应用
要理解Python中的float(input()),可以分两部分。第一,input()用于获取键盘上的输入,该函数的返回值是一个Python字符串str类型的数据——不过输入的是什么;第二,float()函数用于将传递的参数——这里就是input()的返回值,一个字符串——转换为float浮点数的类型。float()函数转换input()的返回值相对于使用int()可以保留相应的精度。
458 1
|
前端开发 API
.NET7之MiniAPI(特别篇):.NET7 Preview3
.NET7之MiniAPI(特别篇):.NET7 Preview3
126 0
|
监控 算法 Java
行业场景限流方案
前言    行业中许多三方服务上线支付宝后,ISV对自身的业务系统没有任何的流量保护,往往会被瞬间的流量,高并发导致系统承载力出现瓶颈,甚至被打垮的挑战。面对行业场景业务的稳定性痛点,我们往往采取限流熔断的机制来保障ISV自身系统在承诺容量下的稳定运行。
3020 1
|
搜索推荐 前端开发 程序员
《2021技术人的百宝黑皮书》下载地址
这里是阿里巴巴淘系技术2021一整年的精华技术内容合集,涵盖了大前端、音视频、推荐系统、 客户端架构、服务端架构、技术质量、以及3D&AI&AR类等多个技术领域,沉淀了双11大促、 淘宝直播、闲⻥APP等多个业务的技术解决方案,并有2021年度必看的技术&行业报告、开源项 目、顶会 paper 等多⻆度知识与价值的输出。
160 0
《2021技术人的百宝黑皮书》下载地址
|
JavaScript 前端开发
Node.js学习笔记(十、函数)
Node.js学习笔记(十、函数)
177 0
|
前端开发 UED .NET
2.ASP.NET全栈开发之在MVC中使用服务端验证
上一章我们在控制台中基本的了解了FluentValidation是如何简洁,优雅的完成了对实体的验证工作,今天我们将在实战项目中去应用它。 首先我们创建一个ASP.NET MVC项目,本人环境是VS2017, 创建成功后通过在Nuget中使用 Install-Package FluentValidation -Version 7.
1285 0
|
Web App开发 Java 测试技术
UI自动化测试框架之Selenium关键字驱动
自动化测试框架demo,用关键字的形式将测试逻辑封装在数据文件中,测试工具解释这些关键字即可对其应用自动化
2004 0