Various run-time errors

Source: Internet
Author: User
Tags error code numeric range reference servervariables client
Error


The earlier part of this chapter shows some of the issues, including how errors occur, how to find errors, how to handle errors, and so on. It is more important now to master the different kinds of errors that can occur and how to distinguish them. The thing to remember is that if you know where to look and what to look for, debugging is easier. At the end of this chapter, you will describe how errors are caught when an error does occur, and prevent the occurrence of errors as early as possible.
Before you learn these things, you should first delve into the different types of run-time and semantic errors that you will definitely encounter at a certain stage, mainly to discuss the following:
· Logical error.
· Script Run-time error.
· ASP and SSI runtime errors.
· Client script error.

7.2.1 Logic Error
Logical errors are often difficult to track in scripts because these errors often result in incorrect results without aborting the Web page. Usually only a few values appear outside the bounds, as seen in the previous array instance, and the errors appear.
However, in the error and debug environment, an algorithm is not as complex as the math class. From a computational standpoint, an algorithm simply refers to a program that completes a task, usually returning a result.
1. Numerical hyper bounds (data overflow)
Typical logic errors typically involve numeric values, or data overflows. For example, if a series of images is known as image1.gif, Image2.gif, and so on, write the following procedure to randomly select an image to display:
<%
' Create a random number between 1 and 5
Intrandom = CInt (Rnd () * 5) # +1
%>

Create a element in a Web page to specify a randomly selected image, for example:

However, if it happens that the program produces the result of the Image6.gif file. In this case, if you had only hoped to get a result in 1~5, the page would be a broken image symbol. The reason is that the CInt function in VBScript takes the value to the nearest integer value. In order to give away the fractional part, you need to use the int or fix function instead of CInt.
2. Precedence of operational symbols
Other types of logical errors have errors that occur as a result of instructions, such as multiplication when you want to use division. Because of the sequence or priority of the mathematical operation symbols in the program, some more difficult to find errors are caused, for example, the following procedure may produce incorrect results.
Intresult = intValue1 * intValue2 + intValue3
Because multiplication has higher operation precedence than addition, it is calculated first. However, if you want to multiply the first and the two digits, you must use parentheses to change the default operation precedence.
Intresult = intValue1 * (intValue2 + intValue3)
VBScript basics| in the VBScript 5.0 document In VBScript operators, the precedence table for all script-run symbols is given. For JScript, in JScript tutorial| JScript basic| The corresponding priority table can also be found under JScript operators. However, the most fundamental principle to remember is that multiplication and division take precedence over addition and subtraction.
3. Managing and formatting String data
From the computational point of view, any structure or function with computational function can be considered as an algorithm. For example, you can take a value from a database to form a string representing the customer's name. This does not cover how to extract data from a database (discussed later in this book). The function of the following program is string concatenation.
Strtitle = {get from database}
strFirstName = {get from database}
Strmiddleinitial = {get from database}
strLastName = {get from database}
Strother = {get from database}

Strprint = strtitle & "." & Strfristname & "& Strmiddleinitial _
& "." & Strstrlastname & "& Strother
Running this program can get the following results:
Ms. Janet C. Clarke MBNA. Bsc.mecheng.
But not everyone, like "Janet," has a middle name. And many people may not have a title, so they may just get:
. Alex. Homer
This is certainly not a fatal error that can cause scripts not to run or generate run-time errors. However, it is not acceptable for users to provide such a script. The best program can check each part of the name before outputting the string.
...
Strprint = ""
If Len (strtitle) Then strprint = strprint & strtitle & "."
If Len (strfirstname) Then strprint = strprint & strFirstName & ""
If Len (strmiddleinitial) Then strprint = strprint & strmiddleinitial & "."
If Len (strlastname) Then strprint = strprint & strLastName
If Len (strother) Then strprint = Strprint & "" & Strother
The above procedure guarantees that spaces and decimal points are added only to the values in the name. If you assign a value to a Strother string only, and you do not assign a value to the other, you will get a space at the beginning. However, the likelihood of this happening is very small. If you have a last name, you can prevent this error by adding only the "other" section.

...
Strprint = ""
If Len (strtitle) Then strprint = strprint & strtitle & "."
If Len (strfirstname) Then strprint = strprint & strFirstName & ""
If Len (strmiddleinitial) Then strprint = strprint & strmiddleinitial & "."
If Len (strLastName) Then
Strprint = strprint & strLastName
If Len (strother) Then strprint = Strprint & "" & Strother
End If
The worst-case scenario is that the result is an empty string that can be checked and aborted.
...
If Len (Strprint) = 0 Then
Response.Clear
Response.End
End If

7.2.2 Script run-time error
A script runtime error occurs when you use a function that does not exist, or if you break the rules used by the scripting language. Many errors are grammatical errors (discussed earlier in this chapter), but many are caused by inconsistent requirements for assigned values and function parameters. For example, use a form to collect dates from users and store them in the database, or otherwise process them. To determine if the date is valid, use the CDate function before inserting the data into the database:
<%
Strdate = Request.Form ("Thedate")
Datdate = CDate (strdate)
...
If a user makes a mistake while completing the form, the program generates a script error, as shown in Figure 7-12:

Viewing the error message, you can see that the error is generated by the scripting engine that executes the program code. The error number is displayed in hexadecimal, which is added by the VBScript error number and the hexadecimal number 0x800a0000 (see chap. 4th), in which the VBScript error number is hexadecimal 0xD, or 13 of the decimal number.
The error numbers returned by most Microsoft technologies (including ASP) are made up of 8-bit hexadecimal digits. The first character is always 8, indicating that this state information is a server error message. Followed by 2-bit 0, followed by the service code. For VBScript and JScript errors, the service code is always "A" and the last 4-bit character is the error number in hexadecimal notation.
If you look at the VBScript document, you will find that the number 13th error is a "Type mismatch" error. Of course, we already know this from the error description that is displayed in the ASP error page. However, we will see later in this chapter that it is very useful to get the error number in the error-handling technique.
Note that in the error Message display window, the server is displaying feedback about the error. The HTTP status code is 500.100 and belongs to "Internal Server Error". In the 4th chapter, when we discuss how ASP custom error pages work, we find that this error often occurs because the error page is loaded. Later in this chapter, you will see how to handle these errors in a Web page.

7.2.3 ASP and SSI Run-time errors
Script errors are found by the scripting engine in use, but ASP DLLs and SSI DLLs can also detect script errors, although they are not related to the scripting engine being used. A typical SSI example is to give a file an incorrect name or path in the #include instruction. The error was discovered by the SSI DLL or ASP, not by the scripting engine. You can see that the error type is "Active Server Pages", and the ASP internal error code is "ASP 0126", as shown in Figure 7-13, whereas in this case the error number is 4005, which indicates that this is a special error defined by an SSI DLL (Ssinc.dll).

Overview of ASP error codes
Table 7-1 is the error code returned for errors that caused failures in the ASP DLL. When this type of error occurs, you can find these error codes in the ASPCode property of the ASPError object.
Table 7-1 ASP Error codes
Error code
Error messages and extended information

ASP0100
Out of Memory (memory overflow)

ASP0101
Unexpected error (function returns Exception_name)

ASP0102
Expecting string input (expecting string input)

ASP0103
Expecting numeric input (expecting digital input)

ASP0104
Operating not allowed (operation not allowed)

ASP0105
Index out of range (array subscript overflow)

ASP0106
Type mismatch (data type mismatch)

ASP0107
Stack Overflow (the amount of data processed exceeds the allowable range)

ASP0115
Unexpected error (a exception_name error that appears in an external object, the script cannot continue to run)

ASP0177
Server.CreateObject falied (Invalid ProgID)

ASP0190
Unexpected error (a trap that occurs when an external object is disposed)

ASP0191
Unexpected error (in the OnStartPage method of an external object that can be caught)

ASP0192
Unexpected error (can be caught in the OnEndPage method of an external object)

ASP0193
OnStartPage Failed (An error occurred in the external object OnStartPage method)

ASP0194
OnEndPage Failed (An error occurred in the OnEndPage method of the external object)

ASP0240
Script Engine Exception (scripting engine throws an exception from object_name exception_name)

ASP0241
CreateObject Exception (Object_name the CreateObject method caused by the exception exception_name)

ASP0242
Query OnStartPage Interface Exception (the exception onstartpage caused by the OnEndPage or Exception_name method that queries the object object_name)

ASP errors usually occur only if the component has a problem or if the server itself has a problem. The most common is the ASP 0177 error and the serious ASP 0115 error when using Server.CreateObject. ASP 0115 errors typically represent errors that occur in component program code, and ASP 0177 errors are usually caused by incorrectly installing components or by ProgID strings that we specify.
7.2.4 Client Script Error
So far, we have learned about the errors from the ASP. However, ASP is often used to create Web pages that contain client-side scripting. If the <SCRIPT> element that contains the client code is not set to the runat= "server" attribute, the ASP will not consider the server and transfer the Web page information to the client without change.
Therefore, if an ASP page is open and a browser error dialog box is displayed, you should not look for errors in the ASP program code on the server side. The browser does not see the ASP program code, so do not recognize any errors, if a dialog box appears on the client, then there must be an error in the client code.
1. Syntax error
If the client-side program code in the Web page has syntax errors, the browser will have a corresponding error when the script is downloaded to the client. The Web page stops executing, although the contents of the Web page can still be loaded normally (unless they are dynamically loaded by these client script code). The user will see a dialog box containing the error details, or a status bar message indicating that the page contains an error.
Modern browsers tend to hide the details of Web script errors and only display a small error icon on the status bar. In IE 4.0 and IE 5.0, the normal error dialog can be activated by setting the Advanced page of the Internet Options dialog box, as shown in Figure 7-14:

Client errors in the processing script code are similar to those on the server side, and are usually easier to download because you can often double-click the Web page directly from the server directory. The only difference is that some server interactions are done by client script, such as data binding with RDS or dynamic loading, without the need to get web pages to see the results in the browser.
2. Run time or semantic error
In client script, you can often encounter syntax errors, and you will often experience run-time or semantic errors. In fact, on the client side, this phenomenon is very common. Because the client cannot control the script's environment like the server side, it is not sure what the user is running on their machine, and in fact the server can only get a general picture from some components such as browser capabilities.
Therefore, scripting programs that use client objects or special versions of scripting languages and properties are likely to not work correctly. However, handling client errors and handling server-side errors is almost the same.
3. Client program code created on the server
A special exception to the "Client dialog for ASP error pages" rule (where there is an error) when the error occurs is to use ASP program code to dynamically create client program code on the server. For example, you might want to evaluate in an ASP, and then pass the data to the script code that runs on the client, perhaps the easiest way is to insert the data as a variable into the script code:
<%
' Get the ' name of my server from the ServerVariables collection
strservernameinasp = Request.ServerVariables ("SERVER_NAME")
%>

<script language= "JScript" runat= "CLIENT" >
<!--hide code from older browsers
var strservername = "<% = strservernameinasp%>";
...
Alert (' Server name is: ' + strservername);
...
Stop Hiding Code
-->
</SCRIPT>
On the client side, after the ASP processes this page, you will get the following:
<script language= "JScript" runat= "CLIENT" >
<!--hide code from older browsers
var strservername = "Wroxbox";
...
Alert (' Server name is: ' + strservername);
...
Stop Hiding Code
-->
</SCRIPT>
You can ignore the runat= "CLIENT" attribute, but adding this can make it clearer when viewing ASP pages that run code.
Thus, if you want to add data from a server-side database to an array of clients in a location, you can implement the following program:
<script language= "JScript" runat= "CLIENT" >
<!--hide code from older browsers
var arrbooks = new Array (a)//highest available index would be

<% ' Start of ASP processing
Intindex = 0
Do as {not in the end of some recordset}
Strtitle = {Get title to database record}
Response.Write "arrbooks[" & CInt (Intindex) & "] = '" _
& Strtitle & "';" & VbCrlf
Intindex = Intindex +1
{Move to next record in database}
Loop
...
Do something in the client with the "array of book titles"
...
Stop Hiding Code
-->
</SCRIPT>
The client code generated by this server-side ASP program code creates an array of title titles when the client runs. The resulting client-side scripting error is now in the browser's error dialog box. The reason for the error is that the Arrbooks-named array is created by JavaScript code when it is run on the client and can accept only 9 titles, while server-side code is likely to produce more than 9 titles, depending on the number of records in the source database. This is equivalent to the following client code:
<script language= "JScript" runat= "CLIENT" >
<!--hide code from older browsers
var arrbooks = new Array (a)//highest available index would be
Arrbooks[0] = ' Instant JavaScript ';
Arrbooks[1] = ' Professional ASP 3.0 programming ';
ARRBOOKS[2] = ' ADO 2.5 programmers Reference ';
...
etc
...
ARRBOOKS[9] = ' ASP techniques for webmasters ';
ARRBOOKS[10] = ' ASP programmers Reference '; <-Client-side error occurs here
ARRBOOKS[11] = ' ADSI CDO programming ';
ARRBOOKS[12] = ' Professional MTS and MSMQ programming ';
...
Do something in the client with the "array of book titles"
...
Stop Hiding Code
-->
</SCRIPT>
This page can work correctly only after it has been modified, either by increasing the size of the array or by controlling the number of records from the database.




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.