{"id":2085,"date":"2020-07-15T10:19:54","date_gmt":"2020-07-15T08:19:54","guid":{"rendered":"https:\/\/alax.info\/blog\/?p=2085"},"modified":"2020-07-15T14:46:16","modified_gmt":"2020-07-15T12:46:16","slug":"another-api-exception-which-should-not-have-been-thrown","status":"publish","type":"post","link":"https:\/\/alax.info\/blog\/2085","title":{"rendered":"Another API exception which should not have been thrown"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">From the documentation on <a href=\"https:\/\/docs.microsoft.com\/en-us\/uwp\/api\/windows.storage.storagefolder.trygetitemasync?view=winrt-19041\"><code>StorageFolder.TryGetItemAsync(String)<\/code> Method<\/a>:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Tries to get the file or folder with the specified name from the current folder. Returns&nbsp;<strong>null<\/strong>&nbsp;instead of raising a&nbsp;<strong>FileNotFoundException<\/strong>&nbsp;if the specified file or folder is not found.<\/p><\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>using IStorageItem = winrt::Windows::Storage::IStorageItem;\nIStorageItem EventStorageItem { m_Configuration.m_ApplicationStorageFolder.TryGetItemAsync(m_FileName).get() };<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Exception thrown at 0x00007FFB06BDA799 (KernelBase.dll) in DownloadIssue.exe: WinRT originate error &#8211; 0x80070002 : &#8216;An item cannot be found with the specified name (Issue-1714684927.tsv).&#8217;.<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">It looks like <code><a href=\"https:\/\/docs.microsoft.com\/en-us\/uwp\/api\/windows.storage.storagefolder.trygetitemasync?view=winrt-19041\">TryGetItemAsync<\/a><\/code> is a wrapper over <code><a href=\"https:\/\/docs.microsoft.com\/en-us\/uwp\/api\/windows.storage.storagefolder.trygetitemasync?view=winrt-19041\">GetItemAsync<\/a><\/code> to suppress exception if file is not found, and it should be the other way around: if there is no file, there is <code>nullptr<\/code> result with no exception. In conjunction with missing mode equivalent to <code><a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/fileapi\/nf-fileapi-createfilea#parameters\">OPEN_ALWAYS<\/a><\/code> it makes it not really convenient to write code free from exceptions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Exceptionless workaround via file query:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#if 1\n\tusing namespace winrt::Windows::Storage;\n\tStorageFile EventStorageFile(nullptr);\n\tSearch::StorageFileQueryResult const FileQueryResult { m_Configuration.m_ApplicationStorageFolder.CreateFileQuery() };\n\tauto const FileVector { FileQueryResult.GetFilesAsync().get() };\n\tfor(auto FileVectorIterator { FileVector.First() }; FileVectorIterator.HasCurrent(); FileVectorIterator.MoveNext())\n\t{\n\t\tauto const StorageFile { FileVectorIterator.Current() };\n\t\tif(StorageFile.IsOfType(StorageItemTypes::File) &amp;&amp; _wcsicmp(StorageFile.Name().c_str(), m_FileName.c_str()) == 0)\n\t\t{\n\t\t\tEventStorageFile = StorageFile;\n\t\t\tbreak;\n\t\t}\n\t}\n\tif(!EventStorageFile)\n\t\treturn;\n#else\n\t\/\/ TODO: Get rid of exception if file is missing\n\tIStorageItem EventStorageItem { m_Configuration.m_ApplicationStorageFolder.TryGetItemAsync(m_FileName).get() };\n\tif(!EventStorageItem)\n\t\treturn;\n\tStorageFile EventStorageFile { EventStorageItem.as&lt;StorageFile>() };\n#endif\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>From the documentation on StorageFolder.TryGetItemAsync(String) Method: Tries to get the file or folder with the specified name from the current folder. Returns&nbsp;null&nbsp;instead of raising a&nbsp;FileNotFoundException&nbsp;if the specified file or folder is not found. Exception thrown at 0x00007FFB06BDA799 (KernelBase.dll) in DownloadIssue.exe: WinRT originate error &#8211; 0x80070002 : &#8216;An item cannot be found with the specified name&hellip; <\/p>\n<p><a class=\"moretag\" href=\"https:\/\/alax.info\/blog\/2085\">Read the full article<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[588,549],"class_list":["post-2085","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c-winrt","tag-uwp"],"_links":{"self":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/2085","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/comments?post=2085"}],"version-history":[{"count":0,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/2085\/revisions"}],"wp:attachment":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/media?parent=2085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/categories?post=2085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/tags?post=2085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}