Click here to Skip to main content
15,909,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Question is that want me to define method as tryparse like that

private bool TryParse(string value,out bool etc)
{
try
{
int.Parse(value);

}
catch (Exception)
{
throw;
}

What I have tried:

private bool TryParse(string value,out bool etc)
{
try
{
int.Parse(value);
etc = true;
return true;
}
catch (Exception)
{etc = false;
return = false;
throw;
}
Posted
Updated 27-Nov-17 23:22pm

1 solution

If you want your code to act like the .net TryParse methods then don't "throw" the exception.

private bool TryParse(string value,out bool etc)
{
    try
    {
        int.Parse(value);
        etc = true;
        return true;
    }
    catch (Exception)
    {etc = false;
    return = false;
}


However your code is really just checking if the string value can be converted to an int. If I was you I'd use the .net method

private bool TryParse(string value,out bool etc)
{
    int x = 0;
    return int.TryParse(value, out x);
}


Despite their name The .net "TryParse" methods don't use try\catch blocks internally as exceptions are expensive to raise, their aim is to parse the text intelligently so an exception doesn't need to be raised.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900