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 aren’t 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.
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...