Statements
217
In a FLA or AS file, enter the following ActionScript in Frame 1 of the Timeline:
import InvalidEmailAddress;
function checkEmail(email:String) {
if (email.indexOf("@") == -1) {
throw new InvalidEmailAddress();
}
}
try {
checkEmail("Joe Smith");
}
catch (e) {
this.createTextField("error_txt", this.getNextHighestDepth(), 0, 0, 100,
22);
error_txt.autoSize = true;
error_txt.text = e.toString();
}
See also
Error
try..catch..finally statement
try {
// ... try block ...
} finally {
// ... finally block ...
}
try {
// ... try block ...
}
catch(
error
[
:ErrorType1
])
// ... catch block ...
}
[catch(
error
[
:ErrorTypeN
]) {
// ... catch block ...
}]
[finally {
// ... finally block ...
}]
Enclose a block of code in which an error can occur, and then respond to the error. If any code
within the
try
code block throws an error (using the
throw
statement), control passes to the
catch
block, if one exists, and then to the
finally
code block, if one exists. The
finally
block always executes, regardless of whether an error was thrown. If code within the
try
block
doesn't throw an error (that is, if the
try
block completes normally), then the code in the
finally
block is still executed. The
finally
block executes even if the
try
block exits using
a
return
statement.
A
try
block must be followed by a
catch
block, a
finally
block, or both. A single
try
block
can have multiple
catch
blocks but only one
finally
block. You can nest
try
blocks as
many levels deep as desired.
The
error
parameter specified in a
catch
handler must be a simple identifier such as
e
or
theException
or
x
. The variable in a
catch
handler can also be typed. When used with
multiple
catch
blocks, typed errors let you catch multiple types of errors thrown from a single
try
block.
Summary of Contents for Flash Lite 2
Page 1: ...Flash Lite 2 x ActionScript Language Reference...
Page 22: ...22 Contents...
Page 244: ...244 ActionScript language elements...
Page 760: ...760 ActionScript classes...