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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Handling Errors in ASP

Status
Not open for further replies.

Mayoor

Programmer
Jan 16, 2004
198
GB
Hi.... I need to trap errors in my ASP code. I understand that there are 2 types of errors in ASP. Database errors and Page errors, so i have written some code to trap both and print them to the screen.

Code:
'Error Handler
 	If Err.Number <> 0 Then
 
 	'Clear response buffer
 	Response.Clear
 
 	
 		If IsObject(conn) Then
 	
 				If conn.Errors.Count > 0 Then	%>
 			
					<B>Database Connection Object</B>
 
 <%					For intLoop = 0 To conn.Errors.Count - 1	%>
 				
						Error No: <%= conn.Errors(intLoop).Number %><BR>
						Description: <%= conn.Errors(intLoop).Description %><BR>
						Source: <%= conn.Errors(intLoop).Source %><BR>
						SQLState: <%= conn.Errors(intLoop).SQLState %><BR>
						count : <%= conn.Errors.Count  %><br>
						NativeError: <%= conn.Errors(intLoop).NativeError %><P>
 
 <%					Next
 				End If
 
 		End If
 
 		If Err.Number <> 0 Then	%>

			<B>Page Error Object</B><BR>
			Error Number <%= Err.Number %><BR>
			Error Description <%= Err.Description %><BR>		
			Source <%= Err.Source %><BR>
			LineNumber <%= Err.Line %><P>

<%		End If
 
 End If
[code]

Does anyone know where I might get something more efficient or maybe help me improve mine as its not the greatest!

It reports some errors and some dont get caught e.t.c.

thanks
 
Sheco provided a skeleton structure for handling errors in this thread thread333-1089924

take a look at it...

-DNG
 
You can still have ADO connection object errors when the VB err object has none.

The way the code above is written, you will only check for connection object errors if you also have a vb error.

I feel like we are only seeing a sliver of the issue... What are you ultimately trying to do here?
 
OK let me explain a bit more. I have a HTML page which allows a user to register. That page then submits and passes the submitted values to another page which captures information and executes various functions which then add the detials to the database.

I would like to capture any errors occuring not only with the DB side but also with the normal execution of the page. I would like to list every single error that occurs on that page and list it in the browser window so that I can email the results to support. Im not too worried about the emailing side as I know how to do that I just want to display all the errors for now. So something that loops through the error object and displays all the errors would be nice.

have googled this all day to no avail. Was hoping some of you geniuses might be able to help....

 
Ah, well one thing you want to take into account is that, while ADO.Errors is a collection that can have more than one error, the regular Err object only has the details of the last error.

So maybe what you need is to periodicly check to see if Err.Number is equal to 0 and, if it is, get the details and write them into your own string or array or something, and then do Err.Clear to reset it to catch the next error.

Then at the bottom of the code you can print out the string or array or whatever you used to keep track of all the errors.
 
OK thanks Sheco. Am I looping through the ADO collection correctly then? It seem that it wonly shows one ADO error despite the fact I have deliberately bodged 3 areas which should bring up 3 ADO errors.
 
If I recall correctly then although can hold multiple errors from each action, it will reset between actions.

You know how, in SQL Server's Query Analyzer program, if you try to run a CREATE PROCEDURE command and it returns more than one error, I think all of those can be in the connection object's error collection if you execute the CREATE PROCEDURE from ADO instead of the Query Analyzer... but if you call the .Execute method twice with 2 different commands then only the errors for the last .Execute would be in there. I think that is right anyway, I'd have to test it to be sure.
 
shco also there is confusion as to where exactly the on error resume next should be placed. Does it only need to be placed once at the top of the page or before every check?
 
Only once is fine unless you use On Error Goto 0 to turn it off.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top