ASP 3.0 Advanced Programming (30)

Source: Internet
Author: User
Programming | Advanced 7th Chapter Debugging and error handling
The basic skills required to use ASP are described earlier, and the other issue in this chapter is what to do when an ASP error occurs, and what happens when an ASP goes wrong. When a carefully choreographed ASP page problem stops executing, the user generally gets only a few useful suggestions, such as clicking the Refresh button, or contacting the Web administrator of the site to tell them that your page is not working properly, and so on.
In addition to providing useful information, this chapter wants to provide a help area. We will describe in detail how errors occur in scripts and pages, the types of errors that can occur, and what causes these errors. What is more important is to discuss how to avoid mistakes as much as possible, and how to deal with them if they cannot be avoided.
Therefore, this chapter will explore the page debugging technology, that is, how to spend a little energy and time to find errors and solve problems.
This chapter includes the following elements:
· The type of error that can occur.
· How to prevent the emergence of various errors.
· How to handle these errors properly if you cannot prevent errors from occurring.
· How to discover and handle scripting errors and other types of errors.
· How to use the custom error page to get the error message.
· How to record errors to monitor our site.
· Create a custom error page and an error log file.
· Provide related online help.
This chapter does not address the various special types of errors that occur when you access a data source using ActiveX Data Objects (ADO). Like many components, ADO provides its own error-handling system, which is discussed in depth in chapter 8th. This chapter starts by discussing the various types of errors that can occur, enabling us to recognize these errors and take appropriate action.
It is said that in the depths of Africa's darkest rainforest, there are such a group of programmers whose code of procedure has never been wrong. Unfortunately, they have never had the pleasure of debugging an application that doesn't work properly. Debugger code is a really fun job, so we have to face this problem, in the process of debugging procedures to test our observation and lateral thinking ability. Most "real world" programmers can experience these pleasures as a good thing.
Of course, some people will say that the debugger is more of a chance than a judgment. It takes a lot of time to debug a faulty program, which in a way can be said to depend on luck. However, if the first step starts at the right place, you may be able to solve the problem more quickly.
But this is not the way the program should be debugged. Theoretically, when a program fails, the error should be tracked in a logical or sequential manner. As a smart and experienced programmer, this is the common method of debugging, only amateurs can change the value of the variable in the program, everywhere add Response.Write statements for debugging.
However, in order to be able to logically track bugs in a program, you must understand the basics of how the error occurred, and more importantly, know where the error occurred so that you can quickly find the appropriate place. This chapter discusses the different kinds of errors that can occur in a program, the different manifestations of errors, and how to record and troubleshoot these errors. It is also important to explain how to avoid these errors.
This chapter starts by describing the different kinds of errors that may occur, and if you think your code doesn't make any mistakes, skip to the next chapter.

7.1 Kinds of Errors

7.1.1 syntax or "compile" error
When we first run the newly written program code, the first type of error that we usually see is "syntax error." This is what is said, the syntax error on the program code. It's like using the wrong grammar in writing so that the reader doesn't know what it means. Interpreters, such as the scripting engine, and compilers require more rigorous and accurate syntax.
Grammatical errors are often the first to appear and need to be excluded. In most cases, the interpreter and the compiler indicate the line number and the character position in the row, and what is missing at the appropriate location. Here's a simple example, as shown in the following procedure:
<%
Response.Write "The repayments for your loan are $" & chrpayment _
& ' per ' & Strinterval &, due on the ' &strday & ' of each '
& Strinterval & "."
%>
We hope to get the following results:
The repayments for your loan are $124.50/month, due on the 12th of each month.
The results are actually as shown in Figure 7-1:

Figure 7-1 Program execution Results 1
Bean-Bean Note:
If your error message does not appear "grammatical error", please change your winnt\help\iishelp\common\500-100.asp as follows (plus two lines of bold):
...
Dim bakcodepage
Bakcodepage = Session.CodePage
Session.CodePage = 936
Response.Write Server.HTMLEncode (Objasperror.category)
If objasperror.aspcode > "" Then Response.Write Server.HTMLEncode ("," & objASPError.ASPCode)
Response.Write Server.HTMLEncode ("0x" & Hex (Objasperror.number) & ")" & "<br>"

If objasperror.aspdescription > "" Then Response.Write Server.HTMLEncode (objasperror.aspdescription) & "<br > "

blnErrorWritten = False

Response.Write "<B>"

If objasperror.description > "" Then Response.Write Server.HTMLEncode (objasperror.description) & "<br>"

' Show the ' Source if it's available and the ' request is ' same machine as IIS
...
The 3rd line in the file is the 2nd line of the Response.Write statement. When you report an error message, the VBScript interpreter ignores the boot spaces and tabs in a row. So after you've finished counting 26 characters, you can find where the syntax is wrong, where a double quote is clearly missing. With double quotes and then run this page, we can get as shown in Figure 7-2:

Figure 7-2 Program execution Results 2
This is another simple mistake. The error actually occurs on line 3rd instead of line 4th. We missed the continuation line ' _ ' at the end of the third line. The program code should be:
<%
Response.Write "The repayments for your loan are $" & chrpayment _
& ' per ' & Strinterval & ', due on the ' &strday & ' of each ' _
& Strinterval & "."
%>
1. Where did the mistake come from?
Note that the script interpreter only points out where the error was found, but it is not necessarily where the error actually appears. In the example above, the syntax for the first three lines is correct and produces the corresponding output, which is precisely the 4th row that causes the problem because the line begins with an illegal character, and the script interpreter does not realize that the line is part of the previous line.
Such errors are common because we typically consider the text content to be output rather than the correct order of double quotes, hyphens ("&" in VBScript), continuation lines, and so on.
Syntax errors caused by a keyword, intrinsic function name spelling error, or an illegal argument list for a function are usually easier to find because the error message hint may indicate the actual location of the error. For example: The following code is to write tomorrow's date to the page.
Response.Write DateAdd (Now (), "D", 1)
The actual results are shown in Figure 7-3:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.