title: Is there a better way to handle a cast exception?
body: <p>Consider the following function:</p>
  
  <pre><code>public enum Operator
  {
      EQUAL = 1,
      GREATER_THAN = 2
  }
  
  public class checkString
  {
      public static bool isValid(string inputString, string checkString, Operator operation)
      {
          switch (operation)
          {
              case Operator.EQUAL:
                  if (inputString == checkString)
                      return true;
  
                  break;
  
              case Operator.GREATER_THAN:
  
                  // Numeric check for greater than
                  try
                  {
                      double inputDouble, checkDouble;
  
                      inputDouble = Convert.ToDouble(inputString);
                      checkDouble = Convert.ToDouble(checkString);
  
                      if (inputDouble &gt; checkDouble)
                          return true;
                  }
                  catch (Exception) 
                  { }
  
                  // Date check for greater than
                  try
                  {
                      DateTime inputDate, checkDate;
  
                      inputDate = DateTime.Parse(inputString);
                      checkDate = DateTime.Parse(inputString);
  
                      if (inputDate. &gt; checkDate)
                          return true;
                  }
                  catch (Exception)
                  { }
  
                  break;
          }
          return false;
      }
  }
  </code></pre>
  
  <p>Parameters</p>
  
  <ul>
  <li>inputString: What we want to evaluate</li>
  <li>checkString: The criteria (value) that the input must evaluate against</li>
  <li>Operator:    Enum for the operation we want to perform</li>
  </ul>
  
  <p>Other things to know</p>
  
  <ul>
  <li>Each line in a file is evaluated against this method to return if a condition has been met</li>
  <li>The process of evaluating the records in the file checks line by line, in one instance that its equal to the condition.  It may also check that the same line is also greater than the condition.  Once the checks are done, it moves to the next record</li>
  <li>There are no additional event listeners hooked up other than whatever defaults are in place, I am not pushing extra data to debug or trace logs</li>
  </ul>
  
  <p>Problem</p>
  
  <p>What people are going to evaluate is unknown to me at any point in this process but I need to be able to check that 'something' (regardless of what) is equal to, greater than or less than something else.  Sure I check other things but I've simplified this function greatly.</p>
  
  <p>That said, using EQUAL or NOT_EQUAL runs fast as can be, processing records in a very large file against said criteria  pretty quick and efficiently.  Once I added the GREATER_THAN logic, its slow... to the point where it takes minutes to process 20 meg files that used to take half a minute tops.</p>
  
  <p>From what I can tell:</p>
  
  <ul>
  <li>Exceptions are thrown all over the place.  There is no guarantee that a field is going to be numeric or of date type.  So I must attempt to cast to these data types to attempt to evaluate the condition</li>
  <li>When exceptions are thrown, the console gets output where I have not instructed it to do so, its sort of automated</li>
  </ul>
  
  <p>Yes I have a lack of experience in this area and am looking to learn more about exception handling and what really happens behind the scenes because when the other 80% of the records are not numeric, thats a lot of exceptions in a 20 meg, 80 thousand record file.</p>
  
  <p>Is there a better way to handle the cast itself to increase efficiency?  I've seen double.Parse / TryParse and can direct cast in front but am not sure which benefits the most.</p>