Interfaces and Events with ActionScript and Flex - Handling Asynchronous Errors
(Page 4 of 4 )
Many objects in ActionScript can potentially throw asynchronous errors. Asynchronous errors are those that occur in response to network operations. For example, if a requested file is not found, the network operation fails asynchronously, and an asynchronous error is thrown. All asynchronous errors are in the form of events, and they use the same event model as standard events. For example, if a URLLoader object attempts to load data outside the Flash Player security sandbox, it dispatches a securityError event. The following example illustrates how to handle error events:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
initialize="initializeHandler(event)">
<mx:Script>
<![CDATA[
private function initializeHandler(event:Event):void {
var loader:URLLoader = new URLLoader();
// In order to test this you'll need to specify a URL of a file that
// exists outside of the security sandbox.
loader.load(new URLRequest("data.xml"));
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,
securityErrorHandler);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
errors.text += event + "\n";
}
]]>
</mx:Script>
<mx:TextArea id="errors" />
</mx:Application>
Using XML
XML is a standard protocol for transferring, storing, and reading data for a variety of purposes, including application initialization parameters, data sets, and remote procedure calls. Flex applications can work with XML by using Flash Player’s native support.
Flash Player 9 supports two mechanisms for working with XML: a legacyXMLDocumentclass and the newXMLclass that implements the ECMAScript for XML (E4X) standard. All XML examples in this book use E4X unless otherwise noted.
Please check back next week for the conclusion to this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of the book Programming Flex 2, written by Chafic Kazoun and Joey Lott (O'Reilly, 2007; ISBN: 059652689X). Check it out today at your favorite bookstore. Buy this book now.
|
|