Flash CS4 Resources
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 it’s 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 compilation 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 doesn’t discuss compile-time errors; for more information on writing code that doesn’t contain compile-time errors, see ActionScript language and syntax and Object-oriented programming in ActionScript. 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.
Run-time errors can be divided into two categories: program errors are mistakes in your ActionScript code, such as specifying the wrong data type for a method parameter; logical errors are mistakes in the logic (the data checking and value manipulation) of your program, such as using the wrong formula to calculate interest rates in a banking application. Again, both of these types of errors can often be detected and corrected ahead of time by diligently testing your application.
Ideally, you’ll want to identify and remove all errors from your application before it is released to end users. However, not all errors can be foreseen or prevented. For example, suppose your ActionScript application loads information from a particular website that is outside of your control. If at some point that website isn’t available, the part of your application that depends on that external data won’t behave correctly. The most important aspect of error handling involves preparing for these unknown cases and handling them gracefully so that users can continue to use your application, or at least get a friendly error message explaining why it isn’t working.
Run-time errors are represented in two ways in ActionScript:
To determine whether a particular method can trigger an error or error event, see the method’s entry in the ActionScript 3.0 Language and Components Reference.
Common error-handling tasks
These are common error-related tasks you might need to perform with your code:
Important concepts and terms
The following reference list contains important terms that you will encounter in this chapter:
Working through in-chapter examples
As you’re working through the chapter, you may want to test some of the example code listings for yourself. Essentially all the code listings in this chapter include the appropriate trace() function call. To test the code listings in this chapter:
Some of the later code listings are more complex and are written as a class. To test these examples:
These techniques for testing example code listings are described in more detail in Testing in-chapter example code listings.