Wednesday, August 4, 2010

Error Hiding and Error Handling

We can hide all the error on the ASP pages just by putting following line on the top of your ASP page:
<%
On Error resume Next
%>

This line simply tells the ASP interpreter, to continue with the executing of the ASP script if there is an error, instead of throwing exception and stopping the script execution.


So what will happen if you follow my advice and add the On Error Resume Next line to your page? Every error generated by your ASP page will be ignored, and as far as your users are concerned everything will look fine. The question is, would everything be OK really and the answer is NO!
Consider the following common web application. We have a simple user registration form, which is saved in our SQL Server database upon submission. If the SQL Server is down at the moment the user submits his form, the ASP script will attempt to generate an ADO connection error, but because we have put the On Error Resume Next statement at the top of our script, this error will be ignored. Hmm, that’s no good. The user will think that his registration form went through, but in fact his registration detail has not being saved. For this it is always a good practice to use error handling along with error hiding
Error handling refers to the programming practice of anticipating and coding for error conditions that may arise when your program runs. Errors in general come in three flavors: compiler errors such as undeclared variables that prevent your code from compiling; user data entry error such as a user entering a negative value where only a positive number is acceptable; and run time errors, that occur when ASP engine cannot correctly execute a program statement.
To trap and handle errors in an ASP application we need to use the ASP Error object
A simple example to handle error in ASP pages is demonstrated below
<%
       dim objErr
       set objErr=Server.GetLastError()

       response.write("ASPCode=" & objErr.ASPCode)
       response.write("")
       response.write("ASPDescription=" & objErr.ASPDescription)
       response.write("")
       response.write("Category=" & objErr.Category)
       response.write("")
       response.write("Column=" & objErr.Column)
       response.write("")
       response.write("Description=" & objErr.Description)
       response.write("")
       response.write("File=" & objErr.File)
       response.write("")
       response.write("Line=" & objErr.Line)
       response.write("")
       response.write("Number=" & objErr.Number)
       response.write("")
       response.write("Source=" & objErr.Source)
  %>

No comments:

Post a Comment