title: Checking SerialPort.BytesToRead vs using TimeoutException in a read loop?
body: <p>I have a .NET C# class which uses <code>System.IO.Ports.SerialPort</code> for serial port communication. It includes a thread which continuously read the serial port in a loop. </p>
  
  <p>I have always thought that using Exceptions as part of normal program flow was bad, but searching the web I see many examples using blocking Read and <code>TimeoutException</code> to wait for incoming data, like this:</p>
  
  <pre><code>while (true)
  {
      try
      {
          serialPort.Read(buffer, offset, count);
          /* Processing code here */
      }
      catch (TimeoutException)
      { }
  }
  </code></pre>
  
  <p>Wouldn't it be better to check <code>SerialPort.BytesToRead()</code> before issuing the <code>Read</code>, like in this example:</p>
  
  <pre><code>while (true)
  {
      if (serialPort.BytesToRead() &lt;= 0)
      {
          /* Wait for event signal or similar. */
      }
  
      serialPort.Read(buffer, offset, count);
      ProcessData();
  }
  </code></pre>