try..catch..finally
737
Example
The following example shows how to create a
try..finally
statement. Because code in the
finally
block is guaranteed to execute, it is typically used to perform any necessary “clean-up”
code after a
try
block executes. In this example, the
finally
block is used to delete an
ActionScript object, regardless of whether an error occurred.
var account = new Account()
try {
var returnVal = account.getAccountInfo();
if(returnVal != 0) {
throw new Error("Error getting account information.");
}
}
finally {
// Delete the 'account' object no matter what.
if(account != null) {
delete account;
}
}
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 displays the error message in a text field using the
Error.toString()
method.
var account = new Account()
try {
var returnVal = account.getAccountInfo();
if(returnVal != 0) {
throw new Error("Error getting account information.");
}
} catch (e) {
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 different types of errors: RecordSetException and
MalformedRecord.
In this 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
Chapter 9,
“Creating Classes with ActionScript 2.0,” on page 155
.)
// 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.";
}
Содержание FLASH MX 2004 - ACTIONSCRIPT
Страница 1: ...ActionScript Reference Guide...
Страница 8: ...8 Contents...
Страница 12: ......
Страница 24: ...24 Chapter 1 What s New in Flash MX 2004 ActionScript...
Страница 54: ...54 Chapter 2 ActionScript Basics...
Страница 80: ...80 Chapter 3 Writing and Debugging Scripts...
Страница 82: ......
Страница 110: ...110 Chapter 5 Creating Interaction with ActionScript...
Страница 112: ......
Страница 120: ...120 Chapter 6 Using the Built In Classes...
Страница 176: ......
Страница 192: ...192 Chapter 10 Working with External Data...
Страница 202: ...202 Chapter 11 Working with External Media...
Страница 204: ......
Страница 782: ...782 Chapter 12 ActionScript Dictionary...
Страница 793: ...Other keys 793 221 222 Key Key code...
Страница 794: ...794 Appendix C Keyboard Keys and Key Code Values...
Страница 798: ...798 Appendix D Writing Scripts for Earlier Versions of Flash Player...
Страница 806: ...806 Appendix E Object Oriented Programming with ActionScript 1...
Страница 816: ...816 Index...