EasyManua.ls Logo

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

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...
About statements 151
To create a custom error:
1. Select File > New and create a new ActionScript file.
2. Select File > Save As and name the file DivideByZeroException.as.
3. Type the following ActionScript into the Script pane:
// In DivideByZeroException.as:
class DivideByZeroException extends Error {
var message:String = "Divide By Zero error";
}
4.
Save the ActionScript file.
5. Create a new Flash document named exception_test.fla in the same directory as the
ActionScript file, and then save the file.
6. Type the following ActionScript into the Actions panel in Frame 1 of the main Timeline:
var n1:Number = 7;
var n2:Number = 0;
try {
if (n2 == 0) {
throw new DivideByZeroException();
} else if (n2 < 0) {
throw new Error("n2 cannot be less than zero");
} else {
trace(n1/n2);
}
} catch (err:DivideByZeroException) {
trace(err.toString());
} catch (err:Error) {
trace("An unknown error occurred; " + err.toString());
}
7.
Save the Flash document and select Control > Test Movie to test the file in the test
environment.
Because the value of
n2 equals 0, Flash throws your custom DivideByZeroException error
class and displays
Divide By Zero error in the Output panel. If you change the value of
n2 in line two from 0 to -1, and retest the Flash document, you would see An unknown
error occurred; n2 cannot be less than zero
in the Output panel. Setting the
value of
n2 to any number greater than 0 causes the result of the division to appear in the
Output panel. For more information on creating custom classes, see Chapter 7, “Classes,
on page 225.

Table of Contents

Related product manuals