Reputation: 23
I'd just like to confirm the logic of this nested try-catch block:
Try
Using dbConn As New SqlConnection With {.ConnectionString = strConnStr}
dbConn.Open()
'Prepare transaction
Try
' Execute transaction
Catch ex As Exception
Try
' Transaction rollback
Catch ex2 As SqlException
' ...
End Try
Finally
dbConn.Dispose()
End Try
End Using
Catch ex As Exception
' ...
End Try
If the database connection fails before the transaction is executed (2nd Try-Catch block), the exception will be caught by the first catch block and will no longer proceed further right?
Is the case in nested Try-Catch blocks, the exceptions are contained inside their own try-catch block? Ie: If the transaction execution fails, it calls the 2nd catch block (which initiates rollback), but it won't call the first catch block yes? Similarly, if the transaction rollback occurs, it will only call its corresponding catch block and not the first two?
Upvotes: 0
Views: 1063
Reputation: 1071
when an exception throws, it will be passed to closest catch block Corresponding to try block to handle it. if there is not any catch block to handle the exception or if Corresponding catch block throws exception, it comes out, if there is any try/catch block, exception passes to parent catch block and so on.
Upvotes: 0