title: Use CloudBlob.ExistsAsync vs catch StorageException.BlobNotFound, in terms of performance?
body: <p>I want to download a file from Azure Blob Storage which may not exist yet. And I'm looking for the most reliable and performant way into handling this. To this end, I've found two options that both work:</p>
  
  <p><strong>Option 1: Use ExistsAsync()</strong><br>
  Duration: This takes roughly 1000~1100ms to complete. </p>
  
  <pre class="lang-cs prettyprint-override"><code>if (await blockBlob.ExistsAsync())
  {
      await blockBlob.DownloadToStreamAsync(ms);
      return ms;
  }
  else
  {
      throw new FileNotFoundException();
  }
  </code></pre>
  
  <p><strong>Option 2: Catch the exception</strong><br>
  Duration: This takes at least +1600ms, everytime. </p>
  
  <pre class="lang-cs prettyprint-override"><code>try
  {
      await blockBlob.DownloadToStreamAsync(ms);                
      return ms;      
  }
  catch (StorageException e)
  {
      // note: there is no 'StorageErrorCodeStrings.BlobNotFound'
      if (e.RequestInformation.ErrorCode == "BlobNotFound")
          throw new FileNotFoundException();
  
      throw;
  }
  </code></pre>
  
  <p>The metrics are done through simple API calls on a webapi, which consumes the above functions and returns an appropriate message. I've manually tested the end-to-end scenario here, through Postman. There is some overhead in this approach of course. But summarized, it seems the ExistsAsync() operation consequently <strong>saves at least 500ms</strong>. At least on my local machine, whilst debugging. Which is a bit remarkable, because the <code>DoesServiceRequest</code> attribute on ExistsAsync() seems to indicate it is another expensive http call that needs to be made.</p>
  
  <p>Also, the <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.windowsazure.storage.blob.cloudblob.existsasync?view=azure-dotnet" rel="nofollow noreferrer">ExistsAsync API docs</a> don't say anything about it's usage or any side-effects.</p>
  
  <p>A blunt conclusion, based on poor man's testing, would therefor lead me to option no. 1, because:</p>
  
  <ul>
  <li>it's faster in debug/localhost (the catch; says nothing about compiled in prod)</li>
  <li>to me it's more eloquent, especially because also the ErrorCode needs manual checking of a particular code</li>
  <li>I would <em>assume</em> the ExistsAsync() operation is there for this exact reason </li>
  </ul>
  
  <p>But here is my <strong>actual question</strong>: is this the correct interpration of the usage of ExistsAsync()?<br>
  E.g. is the "WHY" it exists = to be more efficiënt than simply catching a not found exception, particularly for performance reasons? </p>
  
  <p>Thanks!</p>