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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Posted May 14, 2012, 8:34 PM EDT
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.
Jignesh TrivediRank #16 · 50.3K reputationPosted May 14, 2012, 9:34 PM EDT
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.