165
Chapter 9: Handling errors
To “handle” an error means you build logic into your application that responds to, or fixes, an error, generated either
when an application is compiled or when a compiled application is running. When your application handles errors,
something occurs as a response when the error is encountered, as opposed to no response and whatever process
created the error failing silently. Used correctly, error handling helps shield your application and its users from
otherwise unexpected behavior.
However, error handling is a broad category that includes responding to many kinds of errors that are thrown during
compilation or at run time. This chapter focuses on how to handle run-time errors, the different types of errors that
can be generated, and the advantages of the new error-handling system in ActionScript™ 3.0. This chapter also
explains how to implement your own custom error-handling strategies for your applications.
Contents
Basics of error handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Types of errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Error handling in ActionScript 3.0. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Working with the debugger versions of Flash Player and AIR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Handling synchronous errors in an application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Creating custom error classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Responding to error events and status. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Comparing the Error classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Example: CustomErrors application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Basics of error handling
Introduction to error handling
A run-time error is something that goes wrong in your ActionScript code that stops the ActionScript content from
running in Adobe® Flash® Player or Adobe® AIR™. To ensure that your ActionScript code runs smoothly for users,
you must write code in your application that handles the error—that fixes it, works around it, or at least lets the user
know that its happened. This process is called error handling.
Error handling is a broad category that includes responding to many kinds of errors that are thrown during compi-
lation or at run time. Errors that happen at compile time are often easier to identify—you must fix them in order to
complete the process of creating a SWF file. This chapter doesnt discuss compile-time errors; for more information
on writing code that doesn’t contain compile-time errors, see ActionScript language and syntax” on page 33 and
Object-oriented programming in ActionScript” on page 84. This chapter focuses on run-time errors.
Run-time errors can be more difficult to detect, because in order for them to occur the erroneous code must actually
be run. If a segment of your program has several branches of code, like an
if..then..else statement, you must test
every possible condition, with all the possible input values that real users might use, in order to confirm that your
code is error-free.

Get ADOBE® FLEX® 3: PROGRAMMING ACTIONSCRIPT™ 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.