Question

I am intrested if i use using (that close all connection) inside try catch

try{
     using (var browser = new IE("https://www.bbvanetcash.com/local_kyop/KYOPSolicitarCredenciales.html"))
                    {

                        clsUtils.WriteToLog("Trying to login", true, true);
                        browser.Visible = false;
                        browser.TextField(Find.ByName("cod_emp")).Value = _company;
                        browser.TextField(Find.ByName("cod_usu")).Value = _strUser;
                        browser.TextField(Find.ByName("eai_password")).Value = _strPass;
                        browser.Button(Find.ByClass("grandote estirado azul")).Click();
                        browser.WaitForComplete();
                        clsUtils.WriteToLog("Logged ", true, true);
                        connected = true;
                        var cookie = browser.Eval("document.cookie");
                        CookieContainer Cc = GetCc(cookie);


    } catch (Exception ex)
            {
                Console.WriteLine("(TryToLogin)-->Catching the {0} exception .", ex.GetType());
                return connected;
            }
}

and if one of steps gona fail the using gonna close the connection or it just gona go to catch?

Was it helpful?

Solution

MSDN:

The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Disposein a finally block; in fact, this is how the using statement is translated by the compiler.

OTHER TIPS

yes it does, if you are using a using statement i has to implement the Idispose class.

so if you are using something like below

using (foo)
{
    //...
}

the compiler will interpret like below.

try
{
    //...
}
finally
{
    foo.Dispose();
}

hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top