title: Should TryFoo ever throw an exception?
body: <p>A common pattern in the .NET Framework is the TryXXX pattern (I don't know if that's what they really call it), in which the called method attempts to do something, returning <code>True</code> if it was successful, or <code>False</code> if the operation failed.  A good example is the generic <code>Dictionary.TryGetValue</code> method.</p>
  
  <p>Documentation for these methods say that they will not throw exceptions:  that failure will be reported in the method return value.  All good so far.</p>
  
  <p>I recently encountered two different circumstances in which .NET Framework methods that implement the TryXXX pattern threw exceptions.  See <a href="https://stackoverflow.com/questions/1148278/bug-in-system-random-constructor">Bug in System.Random constructor?</a> and <a href="https://stackoverflow.com/questions/1120283/uri-trycreate-throws-uriformatexception">Uri.TryCreate throws UriFormatException?</a> for details.</p>
  
  <p>In both cases, the <code>TryXXX</code> method called other methods that threw unexpected exceptions, and those exceptions escaped.  My question:  does this break the implied contract of not throwing exceptions?</p>
  
  <p>Put another way, if you were writing <code>TryFoo</code>, would you guarantee that exceptions cannot escape, by writing it like this?</p>
  
  <pre><code>public bool TryFoo(string param, out FooThing foo)
  {
      try
      {
          // do whatever
          return true;
      }
      catch
      {
          foo = null;
          return false;
      }
  }
  </code></pre>
  
  <p>It's tempting, because that guarantees no exceptions will escape, thereby honoring the implied contract.  But it's a bug hider.</p>
  
  <p>My gut feeling, based on my experience, says that this is a really bad idea.  But if <code>TryFoo</code> lets some exceptions escape, then what it's really saying is, "I won't throw any exceptions that I know how to handle," and then the whole idea of the contract "I won't throw exceptions" is thrown out the window.</p>
  
  <p>So, what's your opinion? Should <code>TryFoo</code> handle all exceptions, or just those that it's expecting to happen?  What's your reasoning?</p>