What is the best practice for handling Try Catch when writing automated testing?
<p>I'm currently writing test automation for a web based application for my company. I am utilising C#, Visual Studio test suite, and Selenium to perform testing.</p>

<p>Today I asked the question to my colleague of 'Is there any time where there is too many Try Catch blocks in code?'. His reply was to not work as I am at the minute (see example 1), but to just get the lower level try-catch to throw to the upper level try-catch so that the exception can be written there and the test failed (see example 2).</p>

<p>Example 1:</p>

<p>TestCase.cs</p>

<pre><code>[TestMethod]
public void TestLogin()
{
    Assert.IsTrue(FW_Shared.Perform_Login(FW_Shared.OrgCode, FW_Shared.Username, FW_Shared.Password));
    Console.WriteLine(@"Login Successful");    
}
</code></pre>

<p>FW_Shared.cs</p>

<pre><code>public static class FW_Shared
{
    public static string OrgCode = "Test123";
    public static string Username = "Tester";
    public static string Password = "Password";

    public static void Perform_Login(string OrgCode, string Username, string Password)
    {
        try
        {
            Driver.Url = "http://test.app.com/";
            Driver.FindElement(By.Id("org_code")).SendKeys(OrgCode);
            Driver.FindElement(By.Id("username")).SendKeys(Username);
            Driver.FindElement(By.Id("password")).SendKeys(Password);
            Driver.FindElemenet(By.Id("btnsubmit)).Click();
        }
        catch (Exception ex)
        {
             Console.WriteLine(@"Error occurred logging on: " + ex.ToString());
             return false;
        }
        return true;
    }
}
</code></pre>

<p>Example 2</p>

<p>TestCase.cs</p>

<pre><code>[TestMethod]
public void TestLogin()
{
    try
    {
        Assert.IsTrue(FW_Shared.Perform_Login(FW_Shared.OrgCode, FW_Shared.Username, FW_Shared.Password));
        Console.WriteLine(@"Login Successful");  
    }
    catch (Exception ex)
    {
        Console.WriteLine(@"Exception caught, test failed: " + ex.ToString());
        Assert.Fail();
    }  
}
</code></pre>

<p>FW_Shared.cs</p>

<pre><code>public static class FW_Shared
{
    public static string OrgCode = "Test123";
    public static string Username = "Tester";
    public static string Password = "Password";

    public static void Perform_Login(string OrgCode, string Username, string Password)
    {
        try
        {
            Driver.Url = "http://test.app.com/";
            Driver.FindElement(By.Id("org_code")).SendKeys(OrgCode);
            Driver.FindElement(By.Id("username")).SendKeys(Username);
            Driver.FindElement(By.Id("password")).SendKeys(Password);
            Driver.FindElemenet(By.Id("btnsubmit)).Click();
        }
        catch (Exception)
        {
             throw;
        }
        return true;
    }
}
</code></pre>

<p>Now I know that throwing the Exception to be caught is generally useless in typical coding as you want to handle specific exceptions that are returned, but I want to be able to catch any general web page or element issues so the test can fail on a general issue with the web application. For instance: </p>

<ul>
<li>If the web page returns 503 or 404 issues</li>
<li>If an element is not present on the current web page</li>
<li>If an elements name has changed.</li>
</ul>

<p>There is points in testing other, more complicated parts of the application that I handle unaccessible parts/elements with true/false bool returns and assert that, but since I am referencing multiple function across different classes would sticking with what I have be best, moving to top-level catching of all lower exceptions, or should I be doing something else?</p>