The using
statement expands to a try
finally
block. So when the exception is thrown, the finally
block in the using
statement should execute.
From http://msdn.microsoft.com/en-us/library/yh598w02.aspx:
The using statement ensures that
Dispose is called even if an exception
occurs while you are calling methods
on the object.
What happens next is another matter. As spender points out in his answer, the DataContext
has no obligation to close the connection it is using, nor do you need to be concerned about it (since the DataContext
should properly manage the connection for you).
Under certain conditions, your thrown exception can be swallowed silently by the try
finally
block. See here: http://www.digitallycreated.net/Blog/51/c%23-using-blocks-can-swallow-exceptions. This shouldn't affect you, though.