Parse v. TryParse
Solution 1
Parse
throws an exception if it cannot parse the value, whereas TryParse
returns a bool
indicating whether it succeeded.
TryParse
does not just try
/catch
internally - the whole point of it is that it is implemented without exceptions so that it is fast. In fact the way it is most likely implemented is that internally the Parse
method will call TryParse
and then throw an exception if it returns false
.
In a nutshell, use Parse
if you are sure the value will be valid; otherwise use TryParse
.
Solution 2
If the string can not be converted to an integer, then
-
int.Parse()
will throw an exception -
int.TryParse()
will return false (but not throw an exception)
Solution 3
TryParse and the Exception Tax
Parse throws an exception if the conversion from a string to the specified datatype fails, whereas TryParse explicitly avoids throwing an exception.
Solution 4
The TryParse method allows you to test whether something is parseable. If you try Parse as in the first instance with an invalid int, you'll get an exception while in the TryParse, it returns a boolean letting you know whether the parse succeeded or not.
As a footnote, passing in null to most TryParse methods will throw an exception.
Related videos on Youtube
vishwas kumar
Bad programming is easy. Idiots can learn it in 21 days, even if they are dummies. --Teach Yourself Programming In Ten Years
Updated on July 08, 2022Comments
-
vishwas kumar almost 2 years
What is the difference between Parse() and TryParse()?
int number = int.Parse(textBoxNumber.Text); // The Try-Parse Method int.TryParse(textBoxNumber.Text, out number);
Is there some form of error-checking like a Try-Catch Block?
-
Joel Coehoorn over 15 years"internally the Parse method will call TryParse" Except that Parse pre-dates TryParse by several versions. Of course, they could have moved the core implementation to TryParse...
-
Meydjer Luzzoli over 15 years@Joel - I assumed they would have moved the implementation, but I just had a look with reflector and they are separate implementations with exactly the same code other than one has 'throw ...' and one has 'return false'. I wonder why they aren't consolidated?!
-
Meydjer Luzzoli over 15 yearsAlthough, thinking about it, Parse throws a number of different exceptions so if all it had was a bool from TryParse then it wouldn't know which one to throw.
-
Ray Booysen over 15 yearsTryParse will throw an exception if you pass null in for most integral TryParse methods.
-
Christian Madsen over 14 yearsTryParse does return the value through parameter two which is specified with the out keyword.
-
Christian Madsen over 14 yearsGreat link. I am surprised that no one hasn't yet started the "which one is best or which coding practice should be applied" discussion.
-
Jon over 11 years"use Parse if you are sure the value will be valid". I'd add, "but you acknowledge the possibility you might be wrong". If you were 100% sure it can parse, then you could just as correctly use TryParse which might be faster.
-
Paul Draper about 11 yearsAnd by "different exceptions", @GregBeech means the message, not the class.
-
Paul Draper about 11 yearsBut does
TryParse
returntrue
orfalse
? That's how you'll know if it was "valid". -
Andrew Neely almost 9 yearsThe first code snippit doesn't do anything, since tmpint will already be set to zero if the string is not able to be parsed as an int.
-
Palec almost 7 yearsYou don't need to initialize
startDate
andendDate
asDateTime.TryParse
will always overwrite them withDateTime.MinValue
. If incorrect date representations should be converted to a different value, check the return value ofDateTime.TryParse
and if it is false, set the value explicitly. -
Kiquenet almost 7 yearsUsing
DateTime?
(DateTime nullable) -
Ricardo González about 6 yearsPassing null to basic types (int, double, DateTime, etc.) will NOT throw an exception
-
Alexandru Antochi over 5 yearsWhat if I use int.TryParse(some_method_that_throws_exception(), out int test)? Will it catch any exception or only the ones related to parsing?
-
Rob over 5 years@AlexandruAntochi You should not ask a question as a comment. This will make it almost impossible for others to benefit from useful answers. However, to make it worth your while, the answer to your question is no, int.TryParse will not throw at all. If the method fail to parse, it will only reflect this by a return value of false. This makes it convenient to use if(int.TryParse… to only do something if the parse succeeds.