{"id":102473,"date":"2019-05-03T07:00:00","date_gmt":"2019-05-03T14:00:00","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/oldnewthing\/?p=102473"},"modified":"2019-06-06T17:48:05","modified_gmt":"2019-06-07T00:48:05","slug":"20190503-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190503-00\/?p=102473","title":{"rendered":"Async-Async: Consequences for exceptions"},"content":{"rendered":"<p>As we&#8217;ve been learning, the feature known as Async-Async makes asynchronous operations even more asynchronous by pretending that they started before they actually did. The effect of Async-Async is transparent to properly-written applications, but if you have been breaking the rules, you may notice some changes to behavior. Today we&#8217;ll look at exceptions.<\/p>\n<pre>\/\/ Code in italics is wrong.\r\n\r\nTask task1 = null;\r\nTask task2 = null;\r\ntry\r\n{\r\n    task1 = DoSomethingAsync(arg1);\r\n    task2 = DoSomethingAsync(arg2);\r\n}\r\n<i>catch (ArgumentException ex)<\/i>\r\n{\r\n    \/\/ One of the arguments was invalid.\r\n    return;\r\n}\r\n\r\n\/\/ Wait for the operations to complete.\r\nawait Task.WhenAll(task1, task2);\r\n<\/pre>\n<p>This code &#8220;knows&#8221; that the invalid parameter exception is raised as part of initiating the asynchronous operation, so it catches the exception only at that point.<\/p>\n<p>With Async-Async, the call to <code>Do\u00adSomething\u00adAsync<\/code> returns a fake <code>IAsync\u00adOperation<\/code> immediately, before sending the call to the server. If the server returns an error in response to the operation, it&#8217;s too late to report that error to the client as the return value of <code>Do\u00adSomething\u00adAsync<\/code>. Because, y&#8217;know, time machine.<\/p>\n<p>The exception is instead reported to the completion handler for the <code>IAsync\u00adOperation<\/code>. In the above case, it means that the exception is reported when you <code>await<\/code> the task, rather than when you create the task.<\/p>\n<pre>try\r\n{\r\n    Task task1 = DoSomethingAsync(arg1);\r\n    Task task2 = DoSomethingAsync(arg2);\r\n\r\n    <span style=\"color: blue;\">\/\/ Wait for the operations to complete.\r\n    await Task.WhenAll(task1, task2);<\/span>\r\n}\r\ncatch (ArgumentException ex)\r\n{\r\n    \/\/ One of the arguments was invalid.\r\n    return;\r\n}\r\n<\/pre>\n<p>Again, this is not something that should affect a properly-written program, because you don&#8217;t know when the server is going to do its parameter validation. It might do parameter validation before creating the <code>IAsync\u00adOperation<\/code>, or it might defer doing the parameter validation until later for performance reasons. You need to be prepared for the exception to be generated at either point.<\/p>\n<p>In practice, this is unlikely to be something people stumble across because <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190228-00\/?p=101074\"> Windows Runtime objects generally reserve exceptions for fatal errors<\/a>, so you have no need to try to catch them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The exception doesn&#8217;t pop out until later.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[26],"class_list":["post-102473","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-other"],"acf":[],"blog_post_summary":"<p>The exception doesn&#8217;t pop out until later.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=102473"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102473\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=102473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=102473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=102473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}