title: Why catch and rethrow an exception in C#?
body: <p>I'm looking at the article <em><a href="http://www.codeproject.com/KB/cs/datatransferobject.aspx" rel="noreferrer">C# - Data Transfer Object</a></em> on serializable DTOs.</p>
  
  <p>The article includes this piece of code:</p>
  
  <pre><code>public static string SerializeDTO(DTO dto) {
      try {
          XmlSerializer xmlSer = new XmlSerializer(dto.GetType());
          StringWriter sWriter = new StringWriter();
          xmlSer.Serialize(sWriter, dto);
          return sWriter.ToString();
      }
      catch(Exception ex) {
          throw ex;
      }
  }
  </code></pre>
  
  <p>The rest of the article looks sane and reasonable (to a noob), but that try-catch-throw throws a WtfException... <strong>Isn't this exactly equivalent to not handling exceptions at all?</strong></p>
  
  <p>Ergo:</p>
  
  <pre><code>public static string SerializeDTO(DTO dto) {
      XmlSerializer xmlSer = new XmlSerializer(dto.GetType());
      StringWriter sWriter = new StringWriter();
      xmlSer.Serialize(sWriter, dto);
      return sWriter.ToString();
  }
  </code></pre>
  
  <p>Or am I missing something fundamental about error handling in C#? It's pretty much the same as Java (minus checked exceptions), isn't it? ... That is, they both refined C++.</p>
  
  <p>The Stack Overflow question <em><a href="https://stackoverflow.com/questions/717489">The difference between re-throwing parameter-less catch and not doing anything?</a></em> seems to support my contention that try-catch-throw is-a no-op.</p>
  
  <hr>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Just to summarise for anyone who finds this thread in future...</p>
  
  <p><strong>DO NOT</strong></p>
  
  <pre><code>try {
      // Do stuff that might throw an exception
  }
  catch (Exception e) {
      throw e; // This destroys the strack trace information!
  }
  </code></pre>
  
  <p>The stack trace information can be crucial to identifying the root cause of the problem!</p>
  
  <p><strong>DO</strong></p>
  
  <pre><code>try {
      // Do stuff that might throw an exception
  }
  catch (SqlException e) {
      // Log it
      if (e.ErrorCode != NO_ROW_ERROR) { // filter out NoDataFound.
          // Do special cleanup, like maybe closing the "dirty" database connection.
          throw; // This preserves the stack trace
      }
  }
  catch (IOException e) {
      // Log it
      throw;
  }
  catch (Exception e) {
      // Log it
      throw new DAOException("Excrement occurred", e); // wrapped &amp; chained exceptions (just like java).
  }
  finally {
      // Normal clean goes here (like closing open files).
  }
  </code></pre>
  
  <p>Catch the more specific exceptions before the less specific ones (just like Java).</p>
  
  <hr>
  
  <p><strong>References:</strong></p>
  
  <ul>
  <li><a href="http://msdn.microsoft.com/en-us/library/ms229005.aspx" rel="noreferrer">MSDN - Exception Handling</a></li>
  <li><a href="http://msdn.microsoft.com/en-us/library/0yd65esw.aspx" rel="noreferrer">MSDN - try-catch (C# Reference)</a></li>
  </ul>