Flash
  Home arrow Flash arrow Page 3 - Interfaces and Events with ActionScript an...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
FLASH

Interfaces and Events with ActionScript and Flex
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2008-05-22

    Table of Contents:
  • Interfaces and Events with ActionScript and Flex
  • Handling Events
  • Handling Synchronous Errors
  • Handling Asynchronous Errors

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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>

    More Flash Articles
    More By O'Reilly Media


     

    Buy this book now. 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.

    FLASH ARTICLES

    - 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
    - Organizing Frames and Layers for Flash Anima...
    - Organizing Frames and Layers
    - Using XML and ActionScript with Flex Applica...
    - Interfaces and Events with ActionScript and ...
    - Manipulating Data with ActionScript in Flex ...
    - ActionScript Syntax for Flex Applications







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT