Programming Primer
Licensing And Preface

Standardized Coding Practices

Errors And Exceptions


String DataType

Number DataTypes


Commonly Used Flash Commands

More Topics:





Part 3: Errors And Exceptions

Syntax errors

Computers process information in a very literal way. Although the meaning of a statement might be clear to a human reading the code, to a computer unless it is exactly literal it will be considered to be a “bug in the program”. For example the statement,

trase(“hello world!”);

Will throw an error, Even though any person reading this code knows that the person writing this code more than likely meant to type,

trace(“hello world!”);

Syntax errors are often caused by typo's and are generally easy to fix, once they have been identified.

Logical Errors

Logical errors are errors that do not cause the program to halt, crash or throw an error but will cause the program to act in an unexpected manner or produce unintended results. Logical errors can therefore be difficult to track down and rectify because the program (Flash) does not indicate the specific location of an error.

Common Mistakes resulting in Logical Errors

One of the most common mistakes amongst experienced and novice programmers is incorrect data typing. As noted earlier expressions are of certain types eg strings, numbers etc different data types should generally not be evaluated in the same expression using mathematical operators. For example a string cannot be subtracted, divided etc from or by a number “car”/12 = NaN. It does not make sense in the real world and nor does it make sense in programming. The following code is an example of a logical error in Flash,

firstNumber = "1";

nextNumber = "2";

trace (firstNumber + nextNumber);

One would expect the answer to evaluate to 3, but the answer Flash returns is 12. Flash is adding two expressions that have both been data typed as strings. The process of adding strings together is referred to as concatenation.

A better way of declaring variables is to data type them from the beginning. That way you can be certain of the type of data you are dealing with and if you try evaluating them in the same expression Flash will throw an error. An example of data typing variables at declaration follows

var firstNumber:Number = 1;

var nextNumber:Number = 2;

trace (firstNumber + nextNumber);

The answer now evaluates to 3 as expected.

Just to make things a little more interesting, Flash supports a feature know as data type converting. This might seem like a contradiction to what I just said regarding “strict” data typing, but it can be quite useful. As you probably know data type converting does in fact allow you to use mathematical operators between numbers, certain strings and booleans. To summarize it is not necessary to data type variables but certainly a recommended practice. Here's a poor example of data type conversion,

var firstNumber:String = "1";

var nextNumber:Number = 2;

trace (firstNumber - nextNumber);

returns a value of -1. This is an example of data type conversion. A scenario like this could and should be avoided like so

var firstNumber:String = "1";

var convertedNumber:Number = int(firstNumber);

var nextNumber:Number = 2;

trace (convertedNumber – nextNumber);

a new variable is created and data typed as a number to convert the string to an interger. The trace therefore is not data type converting, but in fact dealing with two numbers.

Next: Variables