How To Create a Custom ASP Error Handling Page

Exonerare de răspundere pentru conținutul retras din baza de cunoștințe

Acest articol a fost scris despre produsele pentru care Microsoft nu mai oferă asistență. Prin urmare, acest articol este oferit „ca atare” și nu mai va fi actualizat.

Summary

When an error occurs in server-side script, Active Server Pages (ASP) stops running the code and displays the error that is associated with the problem that it encountered. However, you may prefer to let the code continue to run despite the error and deal with the problem with custom code.

This article describes how to use error handling to specifically check for a division by zero and to throw out a generic error for any other error encountered. The sample page displays two HTML textboxes, posts back to the same page, and multiplies or divides them based on which button is clicked. For example, if the user types 3 and 0 and clicks Divide, you use a custom error handling page to display a custom message for this. If any other problems occur, such as if the user types alphabetical letters, you can simply display the error number and description, but the page does not fail, and the user can still try again.

Create the Sample Page

  1. From the Start menu, point to Programs, point to Accessories, and then click Notepad.
  2. In Notepad, create a new ASP page named ErrorHandle.asp.
  3. Copy and paste the following code into Notepad:
    <%@ Language=VBScript %>
    <HTML>
    <HEAD>
    </HEAD>
    <BODY>

    <%

    'On Error Resume Next

    If Request.Form("sbmtMultiply")<>"" then 'If multiply is clicked
    Value=Cint(Request.Form("txtA"))*Cint(Request.Form("txtB"))
    End If
    If Request.Form("sbmtDivide")<>"" then 'If divide is clicked
    Value=Cint(Request.Form("txtA"))/Cint(Request.Form("txtB"))
    End If
    If Err.number<>0 then
    If Err.number=11 then '11 is the number that occurs for division by zero.
    Response.Write "This is a custom message. You cannot divide by zero."
    Response.Write "Please type a different value in the second textbox!<p>"
    else
    Response.Write "An Error Has Occurred on this page!<BR>"
    Response.Write "The Error Number is: " & Err.number & "<BR>"
    Response.Write "The Description given is: " & Err.Description & "<BR>"
    End If
    End If
    %>

    <FORM action="" method=POST name=f1>
    A:<INPUT type="text" name=txtA size=5> <BR>B:<INPUT type="text" name=txtB size=5><BR>
    <INPUT type="submit" value="Multiply: A x B" name=sbmtMultiply>
    <INPUT type="submit" value="Divide: A/B" name=sbmtDivide>
    <BR>Result: <INPUT type="text" name=txtResult value="<%=Value%>">
    </FORM>

    </BODY>
    </HTML>
  4. Save ErrorHandle.asp to the home directory of your Web server (for example, C:\Inetpub\Wwwroot).
  5. Start your Web browser (for example, on the Start menu, point to Programs, and then click Internet Explorer).
  6. In your Web browser, type the following address in the address bar, and then press the ENTER key:
    http://servername/ErrorHandle.asp
    where "servername" is the name of the server computer where Microsoft Internet Information Server (IIS) is running. If this is the same computer at which you are currently working, you can use the word "localhost" in place of the computer name.

Test Without Error Handling

  1. Type values that work (for example, 2,2 or 5,5), and click the buttons. The proper result should occur.
  2. Type 0 and 0. This should fail with an ASP message. Notice that you cannot try again; instead, you must refresh the page, or browse to the page again.
  3. Refresh the page. Type e or another character in the box. The page fails (crashes) and returns an ASP error. Again, you must refresh the page to try again.

Test with Error Handling

  1. In Notepad, open ErrorHandle.asp.
  2. Locate the following commented line:
    'On Error Resume Next
  3. Uncomment this line (delete the apostrophe) as follows:
    On Error Resume Next
  4. Save ErrorHandle.asp.
  5. You may need to close the browser window and restart your Web browser to clear any caching. Alternatively, you can press the F5 key to refresh the page.
  6. Type values that work (for example, 2,2 or 5,5), and click the buttons. The proper result should occur.
  7. Type 0 and 0. This should fail with a custom message. Note that your custom error handling code handles the error. Also, notice that you are still able to access the form and try again.
  8. Type e or another character in the box. In this case, the error number and the error description of the problem appear, but the page does not fail (crash), and you can try again.

Understanding the Code

  1. The "On Error Resume Next" line tells the page to continue processing even if it encounters an error. This line logs the problem in the Err object but continues with the rest of the code. If this line is not present (as in the first test), the page immediately stops processing the page and displays an error page with the particular problem that it detected.
  2. The next section of code determines which button is clicked and gives the "Value" variable the appropriate product (multiplied value) or quotient (divided value):
    If Request.Form("sbmtMultiply")<>"" then 'If multiply is clicked
    Value=Cint(Request.Form("txtA"))*Cint(Request.Form("txtB"))
    End If
    If Request.Form("sbmtDivide")<>"" then 'If divide is clicked
    Value=Cint(Request.Form("txtA"))/Cint(Request.Form("txtB"))
    End If
  3. The last section of code checks the values in the Err object to determine how to deal with the error that is encountered. This is the important part. If the Err.number value is zero, no error has been detected, and the code inside is not processed. If Err.number is not zero ("If Err.Number<>0"), the code determines what error has occurred and deals with it accordingly. In this case, the sample code just determines if it is a division by zero. The Err.number value for division by zero is 11, so the code checks for that value as follows:
       If Err.number=11
    In this case, the code just displays a message. You can also call a function or redirect here.

    Because you are in the middle of the loop, the "else" section takes care of any error besides the division by zero. In this case, the code uses the "Description" and "number" properties of the Err object in the message.
    If Err.number<>0 then
    If Err.number=11 then '11 is the number that occurs for division by zero.
    'Run any code here. In this case, display a custom message.
    'Another option would be to redirect to another page, as in
    'Response.Redirect("divbyzero.asp")
    Response.Write "This is a custom message. You cannot divide by zero."
    Response.Write "Please type a different value in the second textbox!<p>"
    else
    Response.Write "An Error Has Occurred on this page!<BR>"
    Response.Write "The Error Number is: " & Err.number & "<BR>"
    Response.Write "The Description given is: " & Err.Description & "<BR>"
    End If
    End If

Troubleshooting

  • To find the number and description for a specific problem that you want to handle, cause the problem in a page that uses "On Error Resume Next" and the following code:
          Response.Write "The Error Number is: " & Err.number & "<BR>"
    Response.Write "The Description given is: " & Err.Description & "<BR>"
  • This code cannot detect performance errors or "hangs."
  • The Err collection only keeps track of the most recent error. For example, note the following code:

    X=Cint("x")
    Y=(3/0)
    Response.Write Err.Description
    This gives the division by zero message and not the type mismatch problem.

References

The ASPError object is introduced in IIS 5.0 and adds some additional error handling options. If you intend to handle the errors yourself and want to be able to continue with the execution of your code, you should still use the Err object. The ASPError object simply adds improved custom error handling support than was previously available for ASP developers. For more information about the additional possibilities that the ASPError object provides, see the following MSDN Web site:

For more information about the Err object, see the following Microsoft Web site:

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

224070 Creating Custom ASP Error Pages








Proprietăți

ID articol: 300043 - Ultima examinare: 8 aug. 2007 - Revizie: 1

Feedback