Ignore errors in Finally section

CJLeit

Freshman
Joined
Feb 1, 2006
Messages
32
Hello,

I have a database connection that I'm closing in the finally section of my error trapping. The problem is sometimes an error occurs before the connection is open so when it hits the finally the connection.close line causes another error to happen. Is there an easy way to ignore errors in the finally section of my error trapping?

Thanks!
 
You would have to put a try catch block within the finally block if you want to supress errors raised there.

It might be easier to check for the likely causes (probably the connection object being null / nothing) and simply don't attempt to close the connection if you haven't got a valid connection.

Out of interest what language / version of .Net are you using? C# all versions and VB.Net from version 2.0 have a using keyword that pretty much does what you are after.
 
Unfortunetly for this project I have to use VS 2003 or else I would try the Using function. Thanks for your reply!
 
I believe it is possible to check the state of the connection before you close that connection. The syntax would likely look like this:

Code:
If (yourConnection.State = ConnectionState.Open) Then
    yourConnection.Close()
End If
 
Back
Top