title: Test if string is a guid without throwing exceptions?
body: <p>I want to try to convert a string to a Guid, but I don't want to rely on catching exceptions (</p>
  
  <ul>
  <li>for performance reasons - exceptions are expensive</li>
  <li>for usability reasons - the debugger pops up </li>
  <li>for design reasons - the expected is not exceptional</li>
  </ul>
  
  <p>In other words the code:</p>
  
  <pre><code>public static Boolean TryStrToGuid(String s, out Guid value)
  {
      try
      {
          value = new Guid(s);
          return true;
      }
      catch (FormatException)
      {
          value = Guid.Empty;
          return false;
      }
  }
  </code></pre>
  
  <p>is not suitable.</p>
  
  <p>I would try using RegEx, but since the guid can be parenthesis wrapped, brace wrapped, none wrapped, makes it hard. </p>
  
  <p>Additionally, I thought certain Guid values are invalid(?)</p>
  
  <hr>
  
  <p><strong>Update 1</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/104850/c-test-if-string-is-a-guid-without-throwing-exceptions#137829">ChristianK</a> had a good idea to catch only <code>FormatException</code>, rather than all. Changed the question's code sample to include suggestion.</p>
  
  <hr>
  
  <p><strong>Update 2</strong></p>
  
  <p>Why worry about thrown exceptions? Am I really expecting invalid GUIDs all that often? </p>
  
  <p>The answer is <em>yes</em>. That is why I am using TryStrToGuid - I <strong>am</strong> expecting bad data.</p>
  
  <p><strong>Example 1</strong> <a href="http://msdn.microsoft.com/en-us/library/cc144096(VS.85).aspx" rel="noreferrer">Namespace extensions can be specified by appending a GUID to a folder name</a>. I might be parsing folder names, checking to see if the text after the final <strong>.</strong> is a GUID.</p>
  
  <pre><code>c:\Program Files
  c:\Program Files.old
  c:\Users
  c:\Users.old
  c:\UserManager.{CE7F5AA5-6832-43FE-BAE1-80D14CD8F666}
  c:\Windows
  c:\Windows.old
  </code></pre>
  
  <p><strong>Example 2</strong> I might be running a heavily used web-server wants to check the validity of some posted back data. I don't want invalid data tying up resources 2-3 orders of magnitude higher than it needs to be.</p>
  
  <p><strong>Example 3</strong> I might be parsing a search expression entered by a user. </p>
  
  <p><img src="https://i.stack.imgur.com/Ghb12.png" alt="enter image description here"></p>
  
  <p>If they enter GUID's I want to process them specially (such as specifically searching for that object, or highlight and format that specific search term in the response text.)</p>
  
  <hr>
  
  <p><strong>Update 3 - Performance benchmarks</strong></p>
  
  <p>Test converting 10,000 good Guids, and 10,000 bad Guids.</p>
  
  <pre><code>Catch FormatException:
     10,000 good:     63,668 ticks
     10,000 bad:   6,435,609 ticks
  
  Regex Pre-Screen with try-catch:
     10,000 good:    637,633 ticks
     10,000 bad:     717,894 ticks
  
  COM Interop CLSIDFromString
     10,000 good:    126,120 ticks
     10,000 bad:      23,134 ticks
  </code></pre>
  
  <p><em>p.s. I shouldn't have to justify a question.</em></p>