int m = Int32.Parse(“abc”); throws an exception???

If the string is not in a valid format, Parse throws an exception.
int m = Int32.Parse(“abc”);
Because, here “abc” is not convertible into int.

So, Will u use Try Catch ???
In C#, we hv TryParse method that does not throw an exception but returns false.
Program:
string s = “abc”;
int k;
bool parsed = Int32.TryParse(s, out k);

if (!parsed)
Console.WriteLine(“Int32.TryParse could not parse ‘{0}’ to an int.\n”, s);

Output:
Int32.TryParse could not parse ‘abc’ to an int.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s