Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Related:

What is the proper way to ensure a SQL connection is closed when an exception is thrown?
like image 987
Brian Kim Avatar asked Feb 05 '09 22:02

Brian Kim


People also ask

Does using statement automatically dispose?

Using-blocks are convenient way to automatically dispose objects that implement IDisposable interface to release resources that garbage collection cannot automatically manage.

What if exception occurs in using block?

The exception is thrown, because there is no catch statement in the compiled code. If you handle the exception within the using block, your object will still be disposed in the compiler-generated finally block.

When the Dispose method is called?

// If disposing equals false, the method has been called by the // runtime from inside the finalizer and you should not reference // other objects. Only unmanaged resources can be disposed. private void Dispose(bool disposing) { // Check to see if Dispose has already been called.

Does using call Dispose C#?

The using declaration calls the Dispose method on the object in the correct way when it goes out of scope. The using statement causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and can't be modified or reassigned.


2 Answers

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.

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
like image 67
Jeff Yates Avatar answered Oct 22 '22 14:10

Jeff Yates


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.
like image 23
Florin Sabau Avatar answered Oct 22 '22 14:10

Florin Sabau