C#C
C#2y ago
11 replies
SWEETPONY

✅ is it okay to use try/catch for logic behavior?

for example here, what is better and why
private string GetHost(string endpoint)
{
    try
    {
        var myUri = new Uri(endpoint as string);
        return myUri.Host;
    }
    catch
    {
        return endpoint;
    }
}

private string GetHost(string endpoint)                               
{                                                                     
    return Uri.TryCreate(endpoint, UriKind.Absolute, out var validUri)
        ? validUri.Host                                               
        : endpoint;                                                   
} 
Was this page helpful?