Await in Catch and Finally
2014-09-07 22:18
183 查看
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
awaitin
catchand
finallyblocks. 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
asyncmethods. The natural way to write this is:
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
awaitin a
catch, so you would have to keep some kind of “error flag” and move the actual error handling logic outside the
catchblock:
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
awaitwithin
catchand
finallyblocks. 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
相关文章推荐
- Exception thrown in catch and finally clause
- Exception thrown in catch and finally clause
- using try catch finally in SQL
- User javascript to catch ctrl in IE and Firefox
- Asynchronous Programming in C# 5.0 using async and await
- finally_clause_and_return_clause_in_java
- In Java, will the code in the finally block be called and run after a return statement is executed?
- [转载] Basic try-catch-finally Exception Handling in Java
- [置顶] Java_try, catch and finally相关解析以及它们与return的爱恨情仇
- 使用try...catch...and or....finally (vbscript or jscript) 处理可能的某些或全部错误
- Catch and Handle WCF Service Exceptions in Silverlight(转)
- [导入]How to keep a local variable in scope across a try and catch block?
- Finally and Return in Java
- 解决错误 Cannot await in the body of a catch clause
- consider this problem again and again, resolve that AndEngine not run in AVD Finally
- Given two strings S1 and S2. Delete from S2 all those characters which occur in S1 also and finally
- Consider this problem again and again, resolve that AndEngine not run in AVD Finally
- try and catch and finally ------- stream 关闭
- Getting started with unit tests in Qt Creator and Catch
- Try-Catch-Finally in PHP