pros and cons of TryCatch versus TryParse

Posted by Vijesh on Stack Overflow See other posts from Stack Overflow or by Vijesh
Published on 2010-03-12T10:01:33Z Indexed on 2010/03/12 10:07 UTC
Read the original article Hit count: 265

Filed under:
|
|
|
|

What are the pros and cons of using either of the following approaches to pulling out a double from an object? Beyond just personal preferences, issues I'm looking for feedback on include ease of debugging, performance, maintainability etc.

public static double GetDouble(object input, double defaultVal)
{
    try
    {
        return Convert.ToDouble(input);
     }
     catch
     {
        return defaultVal;
     }
}

public static double GetDouble(object input, double defaultVal)
{
    double returnVal;
    if (double.TryParse(input.ToString(), out returnVal))
    {
        return returnVal;
    }
else
    {
        return defaultVal;
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about tryparse