Page

9.2.1- Syntax Errors

  by NT Community Manager.
Last Updated  by Jim Minatel.  

PublicCategorized as 09. Error Handling.

Not tagged.
<< 9.2.0- Types of ErrorChapter99.2.2- Logical Errors >>

Syntax Errors

Syntax errors are easily spotted, as they're the ones that cause your program to go belly up. Computers are very precise machines, and the applications that run on them are just as picky. You and I can muddle up words in our sentences and still expect somebody else to make general sense of them. For example, I could say "good morning" to you at one o'clock in the afternoon, and you'd still know what I meant. However your ASP programs can be thrown out by the mildest of typos.

 

<% Reponse.Write "Hello" %>

 

This would provoke an Object Required error, ASP failing completely to recognize that you merely missed an s out. Also if you fail to create a loop structure correctly, such as by missing a Next off the end of the structure:

 

<% For intLoop = 1 to 5

Response.Write "Hello" %>

 

This would cause an error. Or if you fail to close a conditional structure with an End If, or requisite closing statement:

 

<%

If strChoice = "Yes" Then

Response.Write = "Yes"

Else

Response.Write = "No"

%>

This too will generate an error. In fact, ASP will also tell you very quickly about this. As you can imagine there are literally hundreds of different possible types of errors, so we can't go into them all here. Syntax errors are like abuses of the grammatical rules of language. If you wrote, "You was here on time", then your old English tutor would have put a big red line under it at school. Writing a program is almost like writing an English essay, yet you have to get everything correct – one spelling mistake and everything will come tumbling down around your ears. Ok, let's take a look at a few very common causes of syntax errors

Seven Things That Might Cause a Syntax Error

  • Typing Mistakes: We've already mentioned it once, but let's say it again. Check your spelling.
  • Combining two keywords as one brand new keyword: Sounds unlikely? I bet you've seen End If typed as EndIf more times than you care to remember.
  • Construct is not closed properly: Most people take care to close loops and conditions, but it's still easy to be caught out when there are several combined. Look out for something like this:

For intLoop1 = 1 to 10

For intLoop2 = 2 to 20

For intLoop3 = 3 to 30

…Code here…

Next

Next

 

It is not always obvious if the loops are closed 10 pages of code later. However, indenting the code will certainly help.

  • Using the wrong method or property of an object: I find that the Cookies collection is a classic source of this type of error. The following code will not work (I've tried it!):
  • Request.Cookies("Cookie").Expires = Date+1
  • Using a property as a method or a method as a property: It seems obvious, but most experienced programmers have done this at least once:
  • Response.Write = "Hello" or
  • Response.Buffer True
  • Writing to a Read Only property: there are certain properties that just return information. If you tried to assign a value to them, it would generate an error. Back in our Telephone object example, in Chapter 6, the IsConnected property was read only. The same goes for the TotalBytes property of the Request object. The following would not be allowed:
  • Request.TotalBytes = 200
  • Not creating an object properly: If you don't use the keyword Set then the object won't get created. For example in Chapter 6

ObjTelephone = Server.CreateObject("MyTelephone.Telephone")

The one upside is that syntax errors are usually easy to spot, and if you do spot them then they are almost all very easy to correct, unlike the next type of error we will look at.

<< 9.2.0- Types of ErrorChapter99.2.2- Logical Errors >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |