title: Flutter how to handle errors in async / await
body: <p>I want to catch an exception if the app fails to connect to the server (If the server is turned off for example) but not sure how and didn't succeed so far.</p>
  <p>My code:</p>
  <pre><code>static Future&lt;String&gt; communicate(String img, String size) async
  {    
      String request = size.padLeft(10, '0') + img;
      Socket _socket;
  
      await Socket.connect(ip, 9933).then((Socket sock) 
      {
          _socket = sock;
      }).then((_)
      {
          //Send to server
          _socket.add(ascii.encode(request));
          return _socket.first;
      }).then((data)
      {
          //Get answer from server
          response =  ascii.decode(base64.decode(new String.fromCharCodes(data).trim()));
      });
      return response;    
  }
  </code></pre>
  <p>Function call:</p>
  <pre><code>var ans = await communicate(bs64Image, size);
  </code></pre>