Saturday, November 8, 2008

Parse and TryParse Methods - Difference

I have a question in mind for last 1 to 3 months but during this period i was very busy and didn't get time to get answer to my question. The question was
"What is difference between the Parse and TryParese Method ?"
Now i got the time and get answer of my question and the answer to my question is given by Blogging Developer
You can read this article and you will fine what is the difference between the Parse and TryParse method, in this article author also compare them with the convert method but as my question was difference between the Parse and TryParse so here is the difference between them.

Parse: This function takes a string and tries to extract an integer from it and returns the integer. If the string is not a numerical value, the method throws FormatException. If the extracted number is too big, it throws OverflowException. Finally, if the string value is null, it throws ArgumentNullException.
Example: Int32 intValue = Int32.Parse(str);

TryParse: This function is new in .Net 2.0. Since exception handling is very slow, TryParse function returns a boolean indicating if it was able to successfully parse a number instead of throwing an exception. Therefore, you have to pass into TryParse both the string to be parsed and an out parameter to fill in. Using the TryParse static method, you can avoid the exception and ambiguous result when the string is null.

Example : bool isParsed = Int32.TryParse(str, out intValue);

From the above reading you can see that Parse function will throw 3 exceptions which are
1- FormatException

mean when you are using Parse function then there is possibility of 3 exception and when you are using TryParse then there is no such exception only you need to check the Boolean value which is return from the TryParse Function. So my Recommendation is try to Use TryParse and avoid the exceptions.


Tasleem Arif said...

good,its good practice you have mentioned,
just like object.ToString();(which raises exception when objects is null)
and Convert.ToString(); which does not i think

Asim Sajjad said...

Tasleem, you are right object.ToString() will throw exception but Convert.ToStrig() will not null exception for the object which is passed to it

Awais said...

another function worth mentioning is the Convert.ToInt32() ... Int32.Parse() does raise an exception when a null string is passed, but Convert.ToIn32() does not.

but the worst thing about Convert.ToInt32() is that it raises an exception on empty string i.e "" or String.Empty ... strange isn't it ... i mean they could have used their own function String.IsNullOrEmpty() ... but its m$ :s

Awais said...

yes indeed the TryParse() methods comes handy and you don't need those extra exception handling lines, not to mention the overhead involved.

ideally i would like to have another overload in TryParse() method for situations where you only have to check the parseability of a number.

like: bool TryParse (string s)

here i only want to check the convertibility of string s to integer.

but currently i have to pass an extra out parameter which makes its little cumbersome.

one way to get around this is to have an extension method.

check out steve's method []

Asim Sajjad said...

Awais, I have readed that article you have mentioned in your comments, the code is good but if you look at the code the code it did't do any think or you can't check whether conversion is successfull or not as he use TryParse() mathed in his code.
but if you look at this article it is good and fullfill you needs