C# 在 using 语句中抛出异常时,是否仍会调用 Dispose?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/518352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does Dispose still get called when exception is thrown inside of a using statement?
提问by Brian Kim
In the example below, is the connection going to close and disposed when an exception is thrown if it is within a using
statement?
在下面的示例中,如果在using
语句中抛出异常,连接是否会关闭并处理?
using (var conn = new SqlConnection("..."))
{
conn.Open();
// stuff happens here and exception is thrown...
}
I know this code below will make sure that it does, but I'm curious how using statement does it.
我知道下面的这段代码将确保它确实如此,但我很好奇 using 语句是如何做到的。
var conn;
try
{
conn = new SqlConnection("...");
conn.Open();
// stuff happens here and exception is thrown...
}
// catch it or let it bubble up
finally
{
conn.Dispose();
}
Related:
有关的:
What is the proper way to ensure a SQL connection is closed when an exception is thrown?在抛出异常时确保 SQL 连接关闭的正确方法是什么?采纳答案by Jeff Yates
Yes, using
wraps your code in a try/finally block where the finally
portion will call Dispose()
if it exists. It won't, however, call Close()
directly as it only checks for the IDisposable
interface being implemented and hence the Dispose()
method.
是的,using
将您的代码包装在 try/finally 块中,如果存在,该finally
部分将调用该块Dispose()
。但是,它不会Close()
直接调用,因为它只检查IDisposable
正在实现的接口以及Dispose()
方法。
See also:
也可以看看:
- Intercepting an exception inside IDisposable.Dispose
- What is the proper way to ensure a SQL connection is closed when an exception is thrown?
- C# "Using" Syntax
- C# USING keyword - when and when not to use it?
- 'using' statement vs 'try finally'
- What is the C# Using block and why should I use it?
- Disposable Using Pattern
- Does End Using close an open SQL Connection
回答by Florin Sabau
This is how reflector decodes the IL generated by your code:
这是反射器如何解码由您的代码生成的 IL:
private static void Main(string[] args) { SqlConnection conn = new SqlConnection("..."); try { conn.Open(); DoStuff(); } finally { if (conn != null) { conn.Dispose(); } } }
So the answer is yes, it will close the connection if
所以答案是肯定的,如果它会关闭连接
DoStuff()抛出异常。
回答by Chad
Dispose() doesn't get called in this code.
Dispose() 在这段代码中没有被调用。
class Program {
static void Main(string[] args) {
using (SomeClass sc = new SomeClass())
{
string str = sc.DoSomething();
sc.BlowUp();
}
}
}
public class SomeClass : IDisposable {
private System.IO.StreamWriter wtr = null;
public SomeClass() {
string path = System.IO.Path.GetTempFileName();
this.wtr = new System.IO.StreamWriter(path);
this.wtr.WriteLine("SomeClass()");
}
public void BlowUp() {
this.wtr.WriteLine("BlowUp()");
throw new Exception("An exception was thrown.");
}
public string DoSomething() {
this.wtr.WriteLine("DoSomething()");
return "Did something.";
}
public void Dispose() {
this.wtr.WriteLine("Dispose()");
this.wtr.Dispose();
}
}