Home arrow Flash arrow Page 3 - Interfaces and Events with ActionScript and Flex
FLASH

Interfaces and Events with ActionScript and Flex


In this fourth part of a five-part series on using ActionScript in Flex applications, you will learn how to define interfaces, and handle events and errors. It is excerpted from chapter four of the book Programming Flex 2, written by Chafic Kazoun and Joey Lott (O'Reilly, 2007; ISBN: 059652689X). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 9
May 22, 2008
TABLE OF CONTENTS:
  1. · Interfaces and Events with ActionScript and Flex
  2. · Handling Events
  3. · Handling Synchronous Errors
  4. · Handling Asynchronous Errors

print this article
SEARCH DEVARTICLES

Interfaces and Events with ActionScript and Flex - Handling Synchronous Errors
(Page 3 of 4 )

Synchronous errors occur immediately when trying to execute a statement. You can use try/catch/ finally to handle synchronous errors.

When you have some code that may throw runtime errors, surround it with atrystatement:

  try {
   
// Code that might throw errors
  }

You must then include one or morecatchblocks following atry. If the code in thetryblock throws an error, the application attempts to match the error to thecatchblocks in the order in which they appear. Everycatch block must specify the specific type of error that it handles. The application runs the firstcatch block that it encounters to see if it matches the type of error thrown. All error types are eitherflash.errors.Errortypes or subclasses ofError. Therefore, you should try to catch more specific error types first, and more generic types (e.g.,Error) later; for example:

  try {
   
// Code that might throw errors
  }
  catch (error:IOError) {
   
// Code in case the specific error occurs
  }
  catch (error:Error) {

    // Code in case a non-specific error occurs
  }

In addition, you can add afinallyclause that runs regardless of whether thetrystatement is successful:

  try {
   
// Code that might throw errors
  }
  catch (error:IOError) {
   
// Code in case the specific error occurs
  }
  catch (error:Error) {
   
// Code in case a non-specific error occurs
  }
  finally {
   
// Code to run in any case
  }

Most Flash Player and Flex framework classes use asynchronous errors rather than synchronous errors, so the following example may seem impractical, but it does illustrate the syntax for usingtry/catch. Thebrowse()method for aFileReferenceobject opens a browse dialog box that lets the user select a file from his local filesystem. However, Flash Player can display only one browse dialog box at a time. If you callbrowse()while a browse dialog box is already open, it throws aflash.errors.IOErrortype of error. If you donít handle the error, the user receives a notification in a default error dialog box:

  <?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[

        import flash.net.FileReference;

        private function initializeHandler(event:Event):void {
          var file:FileReference = new FileReference();
          file.browse();
          file.browse();
        
}

      ]]>
    </mx:Script>

  </mx:Application>

The following example rewrites the preceding code using error handling:

  <?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[

        import flash.net.FileReference;

        private function initializeHandler(event:Event):void {
          var file:FileReference = new FileReference();
          try {
           
file.browse();
           
file.browse();
          }
          catch(error:Error) {
            errors.text += error + "\n";
          }
        }

      ]]>
    </mx:Script>

    <mx:TextArea id="errors" />

  </mx:Application>


blog comments powered by Disqus
FLASH ARTICLES

- More Top Flash Game Tutorials
- Top Flash Game Tutorials
- Best Flash Photo Gallery Tutorials
- The Top Flash Tutorials for Menus
- 7 Great Flash Tutorials
- Adobe Creative Suite 5.5 Now Available
- Critical Flash Vulnerability Heats Up the Web
- More on Nonpersistent Client-Side Remote Sha...
- Nonpersistent Client-Side Remote Shared Obje...
- Using the Decorator Pattern for a Real Web S...
- Using Concrete Decorator Classes
- Delving More Deeply into the Decorator Patte...
- The Decorator Pattern in Action
- A Simple Decorator Pattern Example
- Decorator Pattern

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials