986
Chapter 2: ActionScript Language Reference
The following example demonstrates a
try..catch
statement. The code within the
try
block is
executed. If an exception is thrown by any code within the
try
block, control passes to the
catch
block, which shows the error message in a text field using the
Error.toString()
method.
In the same directory as Account.as, create a new FLA document and enter the following
ActionScript in Frame 1 of the Timeline:
import Account;
var account:Account = new Account();
try {
var returnVal = account.getAccountInfo();
if (returnVal != 0) {
throw new Error("Error getting account information.");
}
trace("success");
} catch (e) {
this.createTextField("status_txt", this.getNextHighestDepth(), 0, 0, 100,
22);
status_txt.autoSize = true;
status_txt.text = e.toString();
}
The following example shows a
try
code block with multiple, typed
catch
code blocks.
Depending on the type of error that occurred, the
try
code block throws a different type of
object. In this case,
myRecordSet
is an instance of a (hypothetical) class named RecordSet whose
sortRows()
method can throw two types of errors, RecordSetException and MalformedRecord.
In the following example, the RecordSetException and MalformedRecord objects are subclasses of
the Error class. Each is defined in its own AS class file. (For more information, see “Creating
Custom Classes with ActionScript 2.0” in
Using ActionScript in Flash
.)
// In RecordSetException.as:
class RecordSetException extends Error {
var message = "Record set exception occurred.";
}
// In MalformedRecord.as:
class MalformedRecord extends Error {
var message = "Malformed record exception occurred.";
}
Within the RecordSet class’s
sortRows()
method, one of these previously defined error objects is
thrown, depending on the type of exception that occurred. The following example shows how
this code might look:
class RecordSet {
function sortRows() {
var returnVal:Number = randomNum();
if (returnVal == 1) {
throw new RecordSetException();
} else if (returnVal == 2) {
throw new MalformedRecord();
}
}
function randomNum():Number {
return Math.round(Math.random()*10)%3;
Summary of Contents for FLASH MX
Page 1: ...ActionScript Language Reference ...
Page 20: ...20 Contents ...
Page 24: ...24 Chapter 1 Introduction ...
Page 145: ...Button onKeyDown 145 See also Button onKeyUp ...
Page 202: ...202 Chapter 2 ActionScript Language Reference See also break default strict equality switch ...
Page 282: ...282 Chapter 2 ActionScript Language Reference See also break continue while ...
Page 383: ...LoadVars addRequestHeader 383 See also XML addRequestHeader ...
Page 388: ...388 Chapter 2 my_lv load place a valid URL pointing to a text file here ...
Page 463: ...Microphone setRate 463 See also Microphone rate ...
Page 469: ...Microphone silenceLevel 469 See also Microphone gain Microphone setSilenceLevel ...
Page 480: ...480 Chapter 2 ActionScript Language Reference See also Mouse addListener ...
Page 529: ...MovieClip globalToLocal 529 See also MovieClip getBounds MovieClip localToGlobal ...
Page 612: ...612 Chapter 2 ActionScript Language Reference See also MovieClipLoader onLoadStart ...
Page 623: ...NetConnection class 623 See also NetStream class Video attachVideo ...
Page 649: ...Number 649 See also NaN Number class ...
Page 679: ...on 679 on release trace X this _x trace Y this _y stopDrag See also onClipEvent ...
Page 683: ...onUpdate 683 buttonLabel textColor my_mc labelColor ...
Page 788: ...788 Chapter 2 ActionScript Language Reference See also private public ...
Page 1001: ...Video height 1001 my_mc _height my_mc my_video height See also MovieClip _height Video width ...
Page 1022: ...1022 Chapter 2 ActionScript Language Reference See also XML createElement ...
Page 1057: ...XML xmlDecl 1057 See also XML docTypeDecl XML toString ...
Page 1070: ...1070 Chapter 2 ActionScript Language Reference ...
Page 1104: ...1104 Appendix Deprecated Language Elements ...