Microsoft KB Archive/300043

= How To Create a Custom ASP Error Handling Page =

Article ID: 300043

Article Last Modified on 8/8/2007

-

APPLIES TO


 * Microsoft Active Server Pages 4.0

-



This article was previously published under Q300043





IN THIS TASK
SUMMARY
 * Create the Sample Page
 * Test Without Error Handling
 * Test with Error Handling
 * Understanding the Code
 * Troubleshooting

REFERENCES



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.

back to the top

Create the Sample Page
 From the Start menu, point to Programs, point to Accessories, and then click Notepad. In Notepad, create a new ASP page named ErrorHandle.asp.  Copy and paste the following code into Notepad: <%@ Language=VBScript %>    

<%

'On Error Resume Next

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

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

</BODY> </HTML> </li> Save ErrorHandle.asp to the home directory of your Web server (for example, C:\Inetpub\Wwwroot).</li> Start your Web browser (for example, on the Start menu, point to Programs, and then click Internet Explorer).</li> In your Web browser, type the following address in the address bar, and then press the ENTER key:

http://servername/ErrorHandle.asp

where &quot;servername&quot; 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 &quot;localhost&quot; in place of the computer name.</li></ol>

back to the top

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.

back to the top

Test with Error Handling
<ol> In Notepad, open ErrorHandle.asp.</li>  Locate the following commented line: 'On Error Resume Next </li>  Uncomment this line (delete the apostrophe) as follows: On Error Resume Next </li> Save ErrorHandle.asp.</li> 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.</li> Type values that work (for example, 2,2 or 5,5 ), and click the buttons. The proper result should occur.</li> 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.</li> 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.</li></ol>

back to the top

Understanding the Code
<ol> The &quot;On Error Resume Next&quot; 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.</li>  The next section of code determines which button is clicked and gives the &quot;Value&quot; variable the appropriate product (multiplied value) or quotient (divided value): If Request.Form(&quot;sbmtMultiply&quot;)<>&quot;&quot; then 'If multiply is clicked Value=Cint(Request.Form(&quot;txtA&quot;))*Cint(Request.Form(&quot;txtB&quot;)) End If If Request.Form(&quot;sbmtDivide&quot;)<>&quot;&quot; then 'If divide is clicked Value=Cint(Request.Form(&quot;txtA&quot;))/Cint(Request.Form(&quot;txtB&quot;)) End If                   </li>  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 (&quot;If Err.Number<>0&quot;), 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 &quot;else&quot; section takes care of any error besides the division by zero. In this case, the code uses the &quot;Description&quot; and &quot;number&quot; 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(&quot;divbyzero.asp&quot;) Response.Write &quot;This is a custom message. You cannot divide by zero.&quot; Response.Write &quot;Please type a different value in the second textbox! &quot; else Response.Write &quot;An Error Has Occurred on this page!<BR>&quot; Response.Write &quot;The Error Number is: &quot; & Err.number & &quot;<BR>&quot; Response.Write &quot;The Description given is: &quot; & Err.Description & &quot;<BR>&quot; End If End If                   </li></ol>

back to the top

Troubleshooting
<ul>  To find the number and description for a specific problem that you want to handle, cause the problem in a page that uses &quot;On Error Resume Next&quot; and the following code: Response.Write &quot;The Error Number is: &quot; & Err.number & &quot;<BR>&quot; Response.Write &quot;The Description given is: &quot; & Err.Description & &quot;<BR>&quot; </li> This code cannot detect performance errors or &quot;hangs.&quot;</li> <li> The Err collection only keeps track of the most recent error. For example, note the following code: X=Cint(&quot;x&quot;) Y=(3/0) Response.Write Err.Description This gives the division by zero message and not the type mismatch problem. </li></ul>

back to the top

<div class="references_section">