+Bob+ wrote on Tue, 07 Apr 2009 12:00:14 -0400:
On Tue, 7 Apr 2009 11:08:29 -0400, "Bob Barrows"
you can do this:
ErrorHandler:
' clean up
If Not Cnxn1 Is Nothing Then
If Cnxn1.State = adStateOpen Then Cnxn1.Close
End If
Set Cnxn1 = Nothing
They test for the connection object and remove it if it exists and set
it equal to nothing unconditionally. Does that imply that setting an
object equal to nothing does not require a test i.e. it will not throw
an error if the object does not exist?
You can set any variable to Nothing without an error being thrown.
As to error handlers, note that ASP cannot use labels to jump to like the
example you posted. You could however use something like this:
On Error Resume Next
If Not Cnxn1 Is Nothing Then
If Cnxn1.State = adStateOpen Then Cnxn1.Close
End If
Set Cnxn1 = Nothing
Err.Clear
which will let you run those statements without an error being thrown.
However, you then have to watch out for code following throwing errors
without you knowing about it. In my own applications I tend to use On Error
Resume Next right near the top of the application code, and then at critical
intervals within the ASP test for the value of Err.Number, eg,.
If Err.Number <> 0 Then
'call my error handling function here
End If
and the function I have tends to display a curt message, log the error to
the event log for me to check later (I have a number of apps that monitor
the event log for messages and then let me know via various means), and
terminate running the page if required.
Also, wouldn't setting the object to nothing accomplish the same thing?
Or will closing the object first be better in terms of releasing memory
and the d/b connection (real world, not theoretical)
I always make sure I .close an object (in the case of ADO objects that
support that method) before setting it to Nothing. I've never looked
in-depth into whether this makes a huge difference, but I prefer to try and
clean up anything I use to be on the safe side.