Mohgeroth
Mohgeroth

Reputation: 1627

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

Other things to know

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:

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.

Upvotes: 7

Views: 3657

Answers (4)

Brendan Long
Brendan Long

Reputation: 54262

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).

Upvotes: 0

Gustavo Mori
Gustavo Mori

Reputation: 8386

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.

Upvotes: 2

Dustin Davis
Dustin Davis

Reputation: 14585

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");
                    }
            }
        }

Upvotes: 8

dtb
dtb

Reputation: 217351

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
}

Upvotes: 11

Related Questions