Does Dispose still get called when exception is thrown inside of a using statement?

C#asp.netUsing Statement

C# Problem Overview


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 (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.

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();
}

https://stackoverflow.com/questions/141204/what-is-the-proper-way-to-ensure-a-sql-connection-is-closed-when-an-exception-is

C# Solutions


Solution 1 - C#

Solution 2 - C#

This is how reflector decodes the IL generated by your code:

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()
throws an exception.

Solution 3 - C#

Dispose() doesn't get called in this code.

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();
	}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrian KimView Question on Stackoverflow
Solution 1 - C#Jeff YatesView Answer on Stackoverflow
Solution 2 - C#Florin SabauView Answer on Stackoverflow
Solution 3 - C#ChadView Answer on Stackoverflow