Hi all,
please help.....
i want to handle this error "input string was not in correct format" using try and catch block.
Thanks in advance........
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jignesh TrivediPosted May 15, 2012, 1:34 AM
If you handle this error in try catch then use System.FormatException in catch.
but it catch all type format expression error.
Please refer.
http://www.toplinestrategies.com/dotneters/tag/system-formatexception-was-unhandled-by-user-code/?lang=en
http://www.dotnetperls.com/formatexception
hope this help.
Posted May 15, 2012, 12:34 AM
For example,
int.TryParse.
//For failure conversion
int i = default(int);
if (int.TryParse("1h", out i))
{
MessageBox.Show(i.ToString());
}
else
{
MessageBox.Show("Unable to convert the value to Int");
}
The above code will show the message box "Unable to convert the value to Int" message.
//For success conversion
int i = default(int);
if (int.TryParse("1", out i))
{
MessageBox.Show(i.ToString());
}
else
{
MessageBox.Show("Unable to convert the value to Int");
}
The above code will show the message box with "1" message.
If the conversion is success then you'll get the value in "i" otherwise it will execute the "else" part.
If you don't want to use TryParse then you can use try catch as depicted below.
try
{
int i = 0;
i = Convert.ToInt16("1h");
}
catch (FormatException ex)
{
throw ex;
}
Hope this helps you.