Error Handling
ActionScript 3.0 supports runtime error handling. That means that if and when an error occurs, the application can respond to the error in an elegant fashion rather than simply failing to work without any notification to the user. ActionScript 3.0 uses two types of runtime errors: synchronous and asynchronous.
Handling Synchronous Errors
Synchronous errors occur immediately when
trying to execute a statement. Use try/catch/finally to handle synchronous
errors. When you have code that may throw runtime errors,
surround it with a try
statement:
try {
// Code that might throw errors
}You must then include one or more catch blocks following a try. If the code in the try
block throws an error, the application attempts to match the error to
the catch blocks in the order in
which they appear. Every catch block
must specify the specific type of error that it handles. The application
runs the first catch block that it
encounters to see whether it matches the type of error thrown. All error
types are either flash.errors.Error
types or subclasses of Error.
Therefore, you should try to catch more specific error
types first and more generic types (e.g., Error) later; for example:
try {
// Code that might throw errors
}
catch (error:IOError) {
// Code in case the specific error occurs
}
catch (error:Error) {
// Code in case a non-specific error occurs
}In addition, you can add a finally clause that runs regardless of whether the try statement is successful. A finally clause is used ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access