title: Best practices for exception management in Java or C#
body: <p>I'm stuck deciding how to handle exceptions in my application. </p>
  
  <p>Much if my issues with exceptions comes from 1) accessing data via a remote service or 2) deserializing a JSON object. Unfortunately I can't guarantee success for either of these tasks (cut network connection, malformed JSON object that is out of my control). </p>
  
  <p>As a result, if I do encounter an exception I simply catch it within the function and return FALSE to the caller. My logic is that all the caller really cares about is if the task was successful, not why it is wasn't successful.</p>
  
  <p>Here's some sample code (in JAVA) of a typical method)</p>
  
  <pre><code>public boolean doSomething(Object p_somthingToDoOn)
  {
      boolean result = false;
  
      try{
          // if dirty object then clean
          doactualStuffOnObject(p_jsonObject);
  
          //assume success (no exception thrown)
          result = true;
      }
      catch(Exception Ex)
      {
          //don't care about exceptions
          Ex.printStackTrace();
      }
      return result;
  }
  </code></pre>
  
  <p>I think this approach is fine, but I'm really curious to know what the best practices are for managing exceptions (should I really bubble an exception all the way up a call stack?). </p>
  
  <p><strong>In summary of key questions:</strong></p>
  
  <ol>
  <li>Is it okay to just catch exceptions but not bubble them up or formally notifying the system (either via a log or a notification to the user)?</li>
  <li>What best practices are there for exceptions that don't result in everything requiring a try/catch block?</li>
  </ol>
  
  <p><strong>Follow Up/Edit</strong></p>
  
  <p>Thanks for all the feedback, found some excellent sources on exception management online:</p>
  
  <ul>
  <li><a href="http://www.onjava.com/pub/a/onjava/2003/11/19/exceptions.html" rel="noreferrer">Best Practices for Exception Handling | O'Reilly Media</a></li>
  <li><a href="http://www.codeproject.com/KB/architecture/exceptionbestpractices.aspx" rel="noreferrer">Exception Handling Best Practices in .NET</a></li>
  <li><a href="http://web.archive.org/web/20081207002838/http://www.dotnetjunkies.ddj.com/Article/197E493F-BA73-45A2-B39A-4EA282A2E562.dcik" rel="noreferrer">Best Practices: Exception Management</a> (Article now points to archive.org copy)</li>
  <li><a href="http://today.java.net/pub/a/today/2006/04/06/exception-handling-antipatterns.html" rel="noreferrer">Exception-Handling Antipatterns</a></li>
  </ul>
  
  <p>It seems that exception management is one of those things that vary based on context. But most importantly, one should be consistent in how they manage exceptions within a system. </p>
  
  <p>Additionally watch out for code-rot via excessive try/catches or not giving a exception its respect (an exception is warning the system, what else needs to be warned?).</p>
  
  <p>Also, this is a pretty choice comment from <a href="https://stackoverflow.com/users/12460/m3rlinez">m3rLinEz</a>.</p>
  
  <blockquote>
    <p>I tend to agree with Anders Hejlsberg and you that the most callers only
    care if operation is successful or not.</p>
  </blockquote>
  
  <p>From this comment it brings up some questions to think about when dealing with exceptions:</p>
  
  <ul>
  <li>What is the point this exception being thrown?</li>
  <li>How does it make sense to handle it? </li>
  <li>Does the caller really care about the exception or do they just care if the call was successful?</li>
  <li>Is forcing a caller to manage a potential exception graceful?</li>
  <li><strong><em>Are you being respectful to the idoms of the language?</em></strong> 
  
  <ul>
  <li>Do you really need to return a success flag like boolean? Returning boolean (or an int) is more of a C mindset than a Java (in Java you would just handle the exception) one. </li>
  <li>Follow the error management constructs associated with the language :) !</li>
  </ul></li>
  </ul>