|< Day Day Up >|
Every language offers tools for debugging, but historically this is one area that ActionScript has fallen short in. ActionScript 2.0 offers much improved exception handling that allows us to easily handle runtime errors and to separate the error handling from the rest of the application.
One of the most important ways that the error handling has been improved is through the use of data typing. By typing all our variables , Objects, and functions in ActionScript, the compiler is able to generate much more detailed error messages. It also enables the compiler to check for any type mismatches and to easily report these to the developer. Because of the improved, verbose error messages in ActionScript, it is now possible to go line by line through any error messages and make the appropriate changes. This was difficult at best in previous versions.
One of the most important parts of debugging an application is actually viewing the data structures used in the application. The trace function is one way of doing this. trace will allow developers to view Arrays, Strings, Numbers, LoadVars Objects, XML Objects, and the path of a movie clip Object. However, trace will not display all the properties of regular Objects. Rather, the developer will simply see the message [Object Object] in the output window. In addition, the trace function will not display an associative Array which can also be common in building data structures. This is a huge limitation in terms of debugging because it is extremely common to use Arrays of Objects, Objects of Arrays, and other data structures with Object or associative Array properties.
To remedy this issue, we built a custom Dump class that will be able to handle any possible data structure that the developer can pass to it. This class uses one static method dumpThis that the developer can pass any data structure to. This method tests the Object passed in, remembering of course that all Objects descend from the Object class. The dumpThis method determines if the Object is an Array, an Object, or an associative Array, and performs the appropriate looping. A for-i loop is used to loop over a standard Array, while a for-in loop is used to loop through an associative Array or an Object.
We also explored the new try , catch , and finally operators available in ActionScript 2.0. These allow us to easily catch runtime errors that we may not have control of during the application development phase. For example, a runtime error could be that XML was not loaded properly because a server is down or simply that a user did not fill out a form properly. By using the try , catch , and finally operators, in conjunction with conditional logic, we can separate the runtime error handling from the rest of our code. Any code that might possibly fail is surrounded by a try statement. If in fact an error does occur at runtime, control is immediately passed to the catch block where we can place the specific code to handle the error. In addition, ActionScript 2.0 has a new Error class that allows us to return, or throw , different types of error messages. The finally statement is executed in every case, whether or not an error occurs, and is extremely useful for cleaning up because it is executed after the catch statement if an error occurs. Armed with the new tools that ActionScript 2.0 has provided us, many hours previously spent debugging an application can now be put to better use.
|< Day Day Up >|