Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Flex Developer Center /

Global exception and error handling in Flex

by Xavi Beumala

Xavi Beumala
  • rialvalue.com

Content

  • Handling uncaught errors
  • Where to go from here

Created

8 February 2011

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
debugging events Flash Builder 4

Requirements

Prerequisite knowledge

Knowledge of Flex, MXML, and event handlers will help you make the most of this article.

User level

Advanced

Required products

  • Flash Builder 4 (Download trial)
  • Flash Player 10.1

Sample files

  • global-exception-handler.zip

Starting with Flash Player 10.1 and Adobe AIR 2.0, developers can capture unhandled exceptions and errors globally. Note, however, that it's a best practice to take care of the exceptions where they happen. Global handling should only be used for asynchronous exceptions that you can't really control in any other way, or for diagnosing and logging exceptions that are not being caught locally.

In this article I explain how to catch unhandled exceptions or errors globally using the uncaughtError event. The technique I use is declarative and MXML based; it relies on the Flex-specific [Mixin] metadata tag.

Handling uncaught errors

The new API for handing uncaught errors can be used as follows:

loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onUncaughtError); private function onUncaughtError(e:UncaughtErrorEvent):void { // Do something with your error. }

When you're working with Flex and you add an UncaughtErrorEvent.UNCAUGHT_ERROR listener to the loaderInfo object, make sure you do it after FlexEvent.APPLICATION_COMPLETE event has been triggered, otherwise you'll get an error, specifically: Error #1009: Cannot access a property or method of a null object reference .

To simplify this procedure and eliminate the need for all this glue code and configuration code that ends up cluttering the main application class , I created a GlobalExceptionHandler component. The implementation isn't tied to the APPLICATION_COMPLETE event; instead it uses the [Mixin] metadata tag. On top of that you can use it declaratively; for example:

<?xml version="1.0" encoding="utf-8"?> <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:logging="com.adobe.ac.logging.*"> <fx:Declarations> <logging:GlobalExceptionHandler preventDefault="true"> <logging:LogHandlerAction/> <local:TraceHandlerAction/> </logging:GlobalExceptionHandler> </fx:Declarations> </s:WindowedApplication>

The code for this component and a sample project that uses it are included in globalExceptionHandler.zip, the sample file for this article.

Here is the code for GlobalExceptionHandler:

package com.adobe.ac.logging { import flash.display.LoaderInfo; import flash.events.UncaughtErrorEvent; import mx.managers.ISystemManager; [Mixin] [DefaultProperty("handlerActions")] public class GlobalExceptionHandler { private static var loaderInfo:LoaderInfo; [ArrayElementType("com.adobe.ac.logging.GlobalExceptionHandlerAction")] public var handlerActions:Array; public var preventDefault:Boolean; public static function init(sm:ISystemManager):void { loaderInfo = sm.loaderInfo; } public function GlobalExceptionHandler() { loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler); } private function uncaughtErrorHandler(event:UncaughtErrorEvent):void { for each (var action:GlobalExceptionHandlerAction in handlerActions) { action.handle(event.error); } if (preventDefault == true) { event.preventDefault(); } } } }

Here is the code for LogHandlerAction:

package com.adobe.ac.logging { import mx.logging.ILogger; import mx.logging.Log; public class LogHandlerAction implements GlobalExceptionHandlerAction { private static const LOG:ILogger = Log.getLogger("UncaughtException"); public function handle(error:Object):void { if (error is Error) { var errorObj:Error = error as Error; LOG.error("{0}. {1}\n {2}", errorObj.errorID, errorObj.message, errorObj.getStackTrace()); } } }

Where to go from here

For more information on the new error handling APIs, see Christian Cantrell's post on Global Error Handling in AIR 2.0 and Flash Player 10.1.

You may also be interested in two blog posts I've written on related topics:

  • Logging and catching async exceptions during invalidation in Flex
  • Debugging and getting Stack-trace error dialogs with installed AIR applications

Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License+Adobe Commercial Rights

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

More Like This

  • Increasing Flex and ActionScript development productivity with Flash Builder 4.5 and SourceMate 3.0
  • Debugging Flex 4 applications – Part 1: Debugging basics
  • Programming Flex 3 excerpts: Application debugging; embedding Flex applications in a web browser
  • The architecture of Flex and PHP applications
  • Configuring Flash Builder 4.5 and Eclipse PHP development tools
  • Creating Flex components
  • Debugging Flex 4 applications – Part 3: Variables view, watchpoints, and Run to Line
  • Debugging Flex 4 applications – Part 2: Breakpoints
  • Flex 3 Cookbook excerpts: Images, bitmaps, videos, sounds; unit testing with FlexUnit; compiling and debugging
  • Understanding Flex itemEditors – Part 2: Editing events and complex editors

Tutorials & Samples

Tutorials

  • Flex mobile performance checklist
  • Flex and Maven with Flexmojos – Part 3: Journeyman
  • Migrating Flex 3 applications to Flex 4.5 – Part 4

Samples

  • Twitter Trends
  • Flex 4.5 reference applications
  • Mobile Trader Flex app on Android Market

Flex User Forum

More
07/25/2011 Flash Player Debug Issues - Safari 5.1 & Chrome 13
04/22/2012 Loader png - wrong color values in BitmapData
04/22/2012 HTTPService and crossdomain.xml doesn't work as expected
04/23/2012 Memory related crashes in Flex application

Flex Cookbook

More
04/06/2012 How to detect screen resize with a SkinnableComponent
02/29/2012 Embed Stage3D content inside Flex application components
02/15/2012 Custom WorkFlow Component
02/09/2012 Using Camera with a MediaContainer instead of VideoDisplay

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement