Soham Dasgupta
Soham Dasgupta

Reputation: 5199

What happens to the state of a sql connection in using block after the exception encountered is re-thrown?

Try
    xConn.ConnectionString = xConnBuilder.ConnectionString
    xConn.Open()
    Throw New Exception("Something")
Catch ex As Exception
    Throw
Finally
    If xConn.State = ConnectionState.Open Then
        xConn.Close()
    End If
End Try

What happens to the connection object after the exception is thrown and re-thrown maintaining the stack trace, is it closed, because the finally block is not reached in case of exception.

Upvotes: 0

Views: 97

Answers (2)

Girish Sakhare
Girish Sakhare

Reputation: 11

In your sample code the connection will be closed. The finally code block is always executed irrespective of exception occurs or not.

Upvotes: 1

4b0
4b0

Reputation: 22323

Finally block contains the code that must be executed no matter weather there was an error/exception or not.

Upvotes: 3

Related Questions