Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help in error handlingq

Status
Not open for further replies.

thelordoftherings

Programmer
May 16, 2004
616
IL
Hello,

I have this standard code for opening an closing connection to a DB. Can someone please add this code propar error handling?
<%
Dim connection, rs

Sub createConnection()

openstr ="test"
Set connection = Server.CreateObject("ADODB.connection")
connection.Open openstr, "", ""
Set rs = Server.CreateObject("ADODB.Recordset")

End Sub

Sub closeConnection()

rs.close
connection.close
Set rs = Nothing
Set connection = Nothing

End Sub
%>
 
What type of error handling are you attempting to add?

The eaisest method is to declare On Error Resume Next before the code you want to capture errors for, then check occasionally if the err.Number <> 0 Anything other then 0 means an error has occurred for you to process.
A lot of people will create functions just for error checking that include a message to output if an error has occurred. This lets you simply call the funciton everytime you want to check for a possible error. Somehting like:
Code:
Function CheckError(userMessage,boolUseErrDesc)
   If err.Number <> 0 Then
      Response.Clear
      Response.Write "<html><body><span style='color:red;'>"
      Response.Write userMessage
      If boolUserErrDesc Then Response.Write "<br>" & err.Description
      Response.Write "</span><?body></html>"
      Response.End
   End If

   'not really necessary to return a value, but could be used to write a diferant error catching routine
   CheckError = (err.Number <> 0)
End Function

'then in your code you would occasionally call it like this
myConnection.Open myConnectionString
CheckError("The database is currently unavailable, please come back later.",False)

a = cDbl(b)
CheckError("An internal error has occurred.",True)

Hope that helps,

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
Help, the rampaging, spear-waving, rabid network gnomes are after me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top