Search code examples
c#asp.net.netusing

Are using gonna close connection if it fails?


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?


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.