title: Show AlertDialog on HttpClient exception (Xamarin Android)
body: <p>My Xamarin Android app utilizes a Web service, which it connects to using HttpClient. On no connection (for exmaple when the user has no cell nor WiFi connection), an excpetion is thrown. I'm using async/await to get the data from the server. Here is an excerpt from my code:</p>
  
  <pre><code>    public async Task&lt;String&gt; doLogin(string username, string password)
      {
          String url = Constants.loginEndpoint + username + "/" + password + "/";
          var uri = new Uri(string.Format(url, string.Empty));
  
          return_string = "";
  
          try
          {
              var response = await GetAsync(uri);
              if (response.IsSuccessStatusCode)
              {
                  return_string = "success";
                  // Process the positive response here
              else
              { }
          }
          catch (Exception ex)
          {
              throw new ConnectionException();
          }
  
          return return_string;
      }
  </code></pre>
  
  <p>I defined a custon ConnectionException and want to show an AlertDialog to the user to inform them, that the request failed due to no connection. After the user clicks OK I want to close the app. I tried to show the alert dialog in the following way, but it's not working:</p>
  
  <pre><code>    public class ConnectionException : Exception
  {
      public ConnectionException()
      {
          AlertDialog.Builder alert = new AlertDialog.Builder(myApp.Context);
          alert.SetTitle("Failure");
          alert.SetMessage("Request failed. No connection.");
          alert.SetPositiveButton("OK", (senderAlert, args) =&gt;
          {
          });
  
          Dialog dialog = alert.Create();
          dialog.Show();
      }
  
      public ConnectionException(string message)
          : base(message)
      { }
  
      public ConnectionException(string message, Exception innerException)
          : base(message, innerException)
      { }
  }
  </code></pre>
  
  <p>Is this the right approach? Probably not, as it's not working. I would appreciate any help on how to achieve this. Also, I've not given it too much thought, but is this a preferred way to handle such exceptions?</p>