ASP 3.0 Advanced Programming (31)

Source: Internet
Author: User
Tags add date array contains execution tostring
Programming | Advanced 7.1.2 semantics or "run-time" errors
The discovery and handling of grammatical errors is annoying, but in programming there are some really "exciting" other types of errors-semantic errors (semantic error) or "Run-time" errors (runtime error). This type of error is only found when you run a script code or other program. In other words, the complete and valid code has been interpreted or compiled by an interpreter or compiler, resulting in an error in execution. The term "Run-time error" means the result of a semantic error, meaning that such errors exist in the semantics of the code, and they become visible when the code is run.
This distinction stems from the fact that the program compiler or interpreter must establish a description of the internal code before processing the program code, involving a plurality of structure start and end matches to indicate what each structure contains, and then analyze each sentence to know how to execute the sentence. For example, if you have an if Then in your program code ... Else ... End IF structure, the first step of the interpreter or compiler is to analyze which statements are in the "Then" section and which are in the "Else" section. The goal of this step is to determine which branch to execute after testing the IF condition in the structure.
The real difference between compilers (such as those seen in programming languages like Visual Basic and C + +) and interpreters (such as those used for scripting languages like VBScript and JScript) is that the compiler does not attempt to run program code, but after two preprocessing of the source program, form a binary instruction or symbol code, and form an. exe file or a. dll file. The interpreter does not contain code files, but is executed gradually at run time.
1. Error that causes the run to stop
If your program contains a semantic error, you usually get a hint at runtime. If you're lucky, the program stops when the error occurs, which makes it easy to find out where the error is. For example, the following program defines an array of six elements.
<%
Dim arrvalues (5) ' to hold six elements, indexed from 0 to 5
Arrvalues (6) = "Whoops, got an error"
%>
If you attempt to read or set the element value labeled 6, you can get a run-time error, as shown in Figure 7-7:

Figure 7-7 Program execution Results 6
Note that the error type here is "runtime" (equivalent to semantic) errors, not grammatical errors. The error message shows the number of rows in the error and the description of the error, which helps us find the corresponding error more easily. But this is a simple example, and in more complex program code, this error can occur in a program that traverses some values and adds them to an array. As shown below:
<%
Dim arrvalues (5) ' to hold six elements
For intloop = 0 to Intlistcount ' The number of items in some list
Arrvalues (intloop) = Request.Form ("SelectedItems") (Intlistcount)
Next
%>
In this case, it is possible to get too many list entries, or the index of the array is not enough, according to the requirements of the code can be judged that the error, and can increase the size of the array to resolve this error.
<%
Dim arrvalues (a) ' to hold eleven elements
For intloop = 0 to Intlistcount ' The number of items int some list
Arrvalues (intloop) = Request.Form ("SelectedItems") (Intlistcount)
Next
%>
or set the parameters of the loop accordingly to resolve this error.
<%
Dim arrvalues (5) ' to hold six elements
Intarraymax = Intlistcount
If Intarraymax > 5 Then intarraymax = 5
For intloop = 0 to Intarraymax ' only add the ' I six items
Arrvalues (intloop) = Request.Form ("SelectedItems") (Intlistcount)
Next
%>
Many other run-time errors can cause Web page runs to stop, such as the instantiation of some components or objects that failed because of a ProgID error or because the component was not installed properly. In these cases, the result always gives an "ActiveX cannot Create Object" error message, followed by the line number of the Server.CreateObject method called.
2. Error generating Error results
As mentioned above, we may be lucky if we encounter a run-time error that stops the program code. But the other scenario is that the program performs well, as if nothing had happened, resulting in a false result. This is the most difficult to find and solve the error, because there is no awareness of what went wrong. For example, suppose you have a Web page that takes a user's birthday as a date value and displays the date elements separately (you can add them as three entries to a database).
<%
' Get the ' value from the Request and display it
Datbirthdate = Request.Form ("Birthdate")
Response.Write "The value you entered are:" & datbirthdate & "<P>"

' Get the individual date elements
Intday = Day (datbirthdate)
Intmonth = Month (datbirthdate)
Intyear = year (datbirthdate)

' and display them
Response.Write "Day:" & Cstr (Intday) & "<BR>"
Response.Write "Month:" & Cstr (intmonth) & "<BR>"
Response.Write "Year:" & Cstr (intyear) & "<BR>"
%>
Figure 7-8 is the result, which is displayed with the American date style month/day/year, as if everything is fine.

Figure 7-8 showing the birthday screen
However, if you enter an illegal date, or leave the Input text box blank, you get a run-time error, as shown in Figure 7-9:

Figure 7-9 Error Prompt screen
(1) If not a JScript expert
This is not a big problem when looking for errors, because we can quickly find out why there are errors. The fact that the Web stops running helps us track the error. However, unexpected errors may occur. For example, to rewrite the program code with JScript, because it is not a JScript expert, there are some minor errors.
<%
Get the value from the Request and display it
var datbirthdate = new Date (Request.Form ("birthdate"));
Response.Write ("The value you entered are:" + datbirthdate + "<P>");

Get the individual date elements
Intday = Datbirthdate.getday ();
Intmonth = Datbirthdate.getmonth ();
Intyear = Datbirthdate.getyear ();

and display them
Response.Write ("Day:" + intday.tostring () + "<BR>");
Response.Write ("Month:" + intmonth.tostring () + &

[1] [2] [3] Next page



Related Article

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.