C# 如果出现故障,你会关闭连接吗?
如果我在try catch中使用using(关闭所有连接),我很感兴趣C# 如果出现故障,你会关闭连接吗?,c#,asp.net,.net,using,C#,Asp.net,.net,Using,如果我在try catch中使用using(关闭所有连接),我很感兴趣 try{ using (var browser = new IE("https://www.bbvanetcash.com/local_kyop/KYOPSolicitarCredenciales.html")) { clsUtils.WriteToLog("Trying to login", true, true);
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;
}
}
如果其中一个步骤失败,用户将关闭连接,或者它只是去捕捉?:
using
语句确保调用Dispose
,即使在对对象调用方法时发生异常。通过将对象放入try
块中,然后在finally
块中调用Dispose
,可以获得相同的结果;事实上,编译器就是这样翻译using
语句的
是的,如果您使用
使用
语句,则必须实现Idispose类
所以,如果你正在使用下面的东西
using (foo)
{
//...
}
编译器将解释如下
try
{
//...
}
finally
{
foo.Dispose();
}
希望这有帮助