User Guide
Guidelines for Flash applications 125
You might receive data from a web service or from Flash Remoting. In both cases, you could
receive complex data structures, such as arrays, objects, or record sets, which you must parse and
bind appropriately.
Using error handling and debugging
An important part of application development is expecting and handling errors. No matter how
simple your application might seem, there are always users who manage to enter data or interact
with the SWF file in an unexpected way. Your application needs to be robust enough that it can
anticipate certain errors and handle them accordingly.
For example, if you expect users to enter a numeric value into a text box, check that the value is
numeric before you try and store or manipulate the value using code. If the value is not numeric,
it is likely that the code will fail or return an unexpected result because the application cannot
handle this result. Even if the user enters the data in the proper data type, you might have to
validate that the data is usable before processing it. For example, if you did not check the validity
of an integer before trying to perform a mathematical function, you might discover that your
code fails when you try to divide a number by zero, which returns the numerical constant
Infinity.
One of the best ways to perform error handling in Flash MX 2004 is by using the new
try-catch-finally blocks that let you throw and catch custom errors. By creating custom error
classes, you can reuse code throughout your application without having to rewrite error handling
code. For more information on throwing custom errors, see “Error class” in Flash ActionScript
Language Reference. For more information on
try-catch-finally blocks, see
try..catch..finally in Flash ActionScript Language Reference.
Organizing files and storing code
After you decide on a protocol for transferring data, you must consider how to organize your
SWF files, their assets, and ActionScript. How you organize and execute your application depends
greatly on its design, size, and requirements. There are guidelines to help your overall success. The
following are some of the primary things you must consider before you start:
• Do you divide the SWF file into multiple SWF files, and, if so, how should they interact?
• What assets can you share across SWF files?
• What files do you dynamically load?
• How and where do you store ActionScript?
When you develop an application, try to store your server-side code and files in a logical directory
structure, similar to those in an ActionScript 2.0 package. Try to arrange your code this way to
keep it well organized and reduce the risk of the code being overwritten.
For larger applications, encapsulate client-server communication and services in classes. When
you use classes, you benefit in the following ways:
• You can reuse the code in more than one SWF file.
• You can edit code in a central place, and update all SWF files by republishing them.
• You can create a single API that can manipulate different UI elements or other assets that
perform similar functions.