EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Page 150

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
150 Syntax and Language Fundamentals
To use the try..catch..finally block for data validation before dividing some
numbers:
1. Select File > New and then select Flash Document.
2. Select Frame 1 of the Timeline, and then type the following ActionScript in the
Actions panel:
var n1:Number = 7;
var n2:Number = 0;
try {
if (n2 == 0) {
throw new Error("Unable to divide by zero");
}
trace(n1/n2);
} catch (err:Error) {
trace("ERROR! " + err.toString());
} finally {
delete n1;
delete n2;
}
3.
Select Control > Test Movie to test the document.
4. The Output panel displays Unable to divide by zero.
5. Return to the authoring environment and change the following line of code:
var n2:Number = 0;
to
var n2:Number = 2;
6.
Select Control > Enter to test the document again.
If the value of
n2 equals zero, an error is thrown and is caught by the catch block, which
displays a message in the Output panel. If the value of
y is not equal to zero, the Output
panel displays the result of
n1 divided by n2. The finally block executes regardless of
whether an error occurs and deletes the values of the
n1 and n2 variables from the
Flash document.
You arent limited to throwing new instances of the Error class when an error occurs. You
could also extend the Error class to create your own custom errors, as demonstrated in the
following example.

Table of Contents

Related product manuals