J.D. Meier's Blog

Software Engineering, Project Management, and Effectiveness

Performance Guideline: Use TryParse Method to Avoid Unnecessary Exceptions

Performance Guideline: Use TryParse Method to Avoid Unnecessary Exceptions

  • Comments 2

Prashant Bansode, Bhavin Raichura, and Girisha Gadikere teamed up with Claudio Caldato (CLR team) to create some new performance guidelines for .NET Framework 2.0.  The guidelines use our new guideline template.

 ...


Use TryParse Method to Avoid Unnecessary Exceptions

Applies to

  • NET 2.0

What to Do
Use TryParse Method instead of Parse Method for converting string input to a valid .Net data type. For example, use TryParse method before converting a string Input to integer data type.

Why
The Parse method will throw exception - ArgumentNullexception or FormatException or OverflowException, if the string representation cannot be converted to the respective data type.

Unnecessary Throwing Exceptions and Handling the same such as above has a negative impact on the performance of the application. The TryParse method does not throw an exception if the conversion fails instead it returns false, and hence saves exception handling related performance hit.

When
If it is required to convert a string representation of a data type to a valid .Net data type, use TryParse method instead of calling the Parse method to avoid unnecessary exception.

How
The following code snippet illustrates how to use TryParse method :

    ...
    Int32 intResult;
    if (Int32.TryParse(strData, intResult))
    {
       // process intResult result
    }
    else
    {
      //error handling
    }
    ...

Problem Example
Consider a Windows Forms application for creating an Invoice. The application takes user inputs for multiple items as product name, quantity, price per unit and date of purchase. The user provides these inputs in the text boxes. The user can enter multiple items in an invoice at a given time and then finally submit the data for automatic billing calculation. The application internally needs to convert the string input data to integer (assume for simplicity). If the user enters invalid data in the text box, the system will throw an exception. This has adverse impact on performance of the application.

    ...
    private Int32 ConvertToInt(string strData)
    {
        try
        {
              return Int32.Parse(strData);
        }
        catch (exception ex)
        {
              return 0; //some default value
        }
    }
    ...

Solution Example
Consider a Windows Forms application for creating an Invoice. The application takes user inputs for multiple items as product name, quantity, price per unit and date of purchase. The user provides these inputs in the text boxes. The user can enter multiple items in an invoice at a given time and then finally submit the data for automatic billing calculation. The application internally needs to convert the string input data to integer (assume for simplicity). If the user enters invalid data in the text box, the system will not throw unnecessary exceptions and hence improves the application performance.

    ...
    private Int32 ConvertToInt(string strData)
    {
        Int32 intResult;
        if (Int32.TryParse(strData, intResult))
        {
            return intResult;
        }
         return o;  //some default value
    }
    ...

Additional Resources

 

Page 1 of 1 (2 items)