开发者

Is there a better way to handle a cast exception?

Consider the following function:

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 > checkDouble)
                        return true;
                }
                catch (Exception) 
                { }

                // Date check for greater than
                try
                {
                    DateTime inputDate, checkDate;

                    inputDate = DateTime.Parse(inputString);
                    checkDate = DateTime.Parse(inputString);

                    if (inputDate. > checkDate)
                        return true;
                }
                catch (Exception)
                { }

                break;
        }
        return false;
    }
}

Parameters

  • inputString: What we want to evaluate
  • checkString: The criteria (value) that the input must evaluate against
  • Operator: Enum for the operation we want to perform

Other things to know

  • Each line in a file is evaluated against this method to return if a condition has been met
  • 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
  • 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

Problem

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.

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.

From what I can tell:

  • Exceptions are thrown all over the place. There is no guarantee that a field is going to be numeric or of date开发者_Python百科 type. So I must attempt to cast to these data types to attempt to evaluate the condition
  • When exceptions are thrown, the console gets output where I have not instructed it to do so, its sort of automated

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.

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.


Use double.TryParse and DateTime.TryParse instead of Convert.ToDouble and DateTime.Parse respectively.

Example:

double result;

if (double.TryParse(someString, out result))
{
    Console.WriteLine(result);
}
else
{
    // not a valid double
}


You can use TryParse() on those data types. Exceptions are messy and expensive. TryParse will return true/false if it worked or not while NOT throwing an exception. So you can just check the results of the call. Much more efficient than exceptions.

Convert.ToDouble() and Double.Parse() will throw exceptions.

try this code. It isn't the best, but it's better than what you have now considering you don't know what the type could be:

public static bool isValid(string inputString, string checkString, Operator operation)     
        {         
            double dblTmp1;
            double dblTmp2;

            if (Double.TryParse(inputString, out dblTmp1) && double.TryParse(checkString, out dblTmp2))
            {
                return Compare<Double>(dblTmp1, dblTmp1, operation);
            }

            DateTime dtTmp1;
            DateTime dtTmp2;
            if (DateTime.TryParse(inputString, out dtTmp1) && DateTime.TryParse(checkString, out dtTmp2))
            {
                return Compare<DateTime>(dtTmp1, dtTmp2, operation);
            }

            throw new InvalidOperationException("Unknown type");

        }

        public static bool Compare<T>(T obj1, T obj2, Operator operation) where T : IComparable      
        {
            switch (operation)
            {
                case Operator.EQUAL:
                    {
                        return obj1.Equals(obj2);
                    }
                case Operator.GREATER_THAN:
                    {
                        return obj1.CompareTo(obj2) > 0;
                    }
                default:
                    {
                        throw new InvalidOperationException("Unknown operation");
                    }
            }
        }


Keep in mind that using exceptions slows down your program, because behind the scenes the runtime is creating an exception stack in order to be able to unwind this in case an exception is thrown. This stack is maintained regardless of whether your program throws or not, and that overhead is what slows you down the most.


The other answers are probably the best solution in this case, but in the general case you can improve your solution by catching the specific exception, which is probably NumberFormatException or ClassCastException. Catching Exception can cause all kinds of annoying, hard to trace problems (since you're not logging the exception).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜