Global error handling with Flash Player 10.1

Since the official release of Flash Player 10.1 is out, now might be a good time to start implementing the global error handler.

When this is written, flash builder 4 doesn’t have a native way that lets you use it, so we have to do a little mix. (Update: The update is out.)
The global error handler works by adding an event to the uncaughtErrorEvents property of the loaderInfo of the application.
There are currently two methods of getting it to work.

Method 1 – The backwards compatible one:

Here the code doesn’t crash in flash player 9/10, but the error handling will only work in 10.1.

if(loaderInfo.hasOwnProperty("uncaughtErrorEvents")){
	IEventDispatcher(loaderInfo["uncaughtErrorEvents"]) .addEventListener("uncaughtError", uncaughtErrorHandler);
}
private function uncaughtErrorHandler(e:Event):void {
	trace("Global error:", e);
}

Method 2 – The type safe one:

Get the Flex 4.1 SDK if you haven’t already and choose that one as your project’s SDK.

Now you can use the new global error handling like it was meant to be used:

import flash.events.UncaughtErrorEvent;

loaderInfo.uncaughtErrorEvents.addEventListener( UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);

private function uncaughtErrorHandler( e:UncaughtErrorEvent):void {
	trace("Global error:", e);
}

Tags: , , , , , , , , , , , , , , ,

7 Responses to “Global error handling with Flash Player 10.1”

  1. Dan Mitchell Says:

    First, thanks for the post; second, I’ve spent the morning wrestling with this stuff, and can add a couple of extra notes:

    1. If you install SDK 4.1, that now points at Flash 10.1 / Air 2.0, so adding the “correct” code above to a new Flash Builder 4 project compiles and runs properly.

    2. However, it’s not trivial to add GEH to an existing project — anything that starts off with (say)

    <mx:WindowedApplication
    xmlns:mx="http://www.adobe.com/2006/mxml&quot;

    won't work, because the 2006 in the namespace there winds up with uncaughtErrorEvents not existing (using either code sample), even when using the 10.1 runtime. Switching to the new namespace means a significant porting effort to handle all the changes; blocks live somewhere else, WindowedApplication has different attributes, etc, etc.

  2. Flassari Says:

    Thanks, I’ve updated the post to include the Flex 4.1 SDK. Much simpler =)
    Too bad about the existing project thing..

  3. Alex Says:

    Thanks for the post.

    We’re trying unsuccessfully to get global exception handling to work with 10.1 AND not force the user to upgrade from flash player 9.

    1. we have a hello world app which when you click a button throws an exception
    2. we have declared the event listener using the following:
    if (loaderInfo.hasOwnProperty(“uncaughtErrorEvents”))
    IEventDispatcher(loaderInfo[“uncaughtErrorEvents”]).addEventListener(“uncaughtError”, uncaughtErrorHandler);

    If we target the compiler to flash player 9.0.124 – then when running with 10.1 – no listener ever gets added

    If we target the compiler to flash player 10.x – then we get the error event but any 9.0.124 user must upgrade to 10

    Do you know what we’re doing wrong? I don’t understand why you’d need to do step 2 (your method 1) above if it couldn’t be backwards compatible. Is it backwards compatible to 10.0 only? not 9.x?

  4. Eyal Says:

    Thanks for the post.
    I tried several methods but I couldn’t get the GEH to work with Flex SDK 3.x (even when I compile it with 10.1). It only works when I use SDK 4.1 and set the compiler to 10.1.

    I opened a JIRA bug:
    http://bugs.adobe.com/jira/browse/FP-5349

    And the response was that this is a SDK 4 feature. Is this true? Do I have to upgrade to Flex 4.1 to use the GEH?

  5. Flassari Says:

    You might have to target Flash Player 10.1 in the compiler settings, even though you’re using an older Flex SDK.

  6. guya Says:

    Eyal,

    I’ve managed to make it work on Flex 3.x without using RSL, trying to figure how to make it work with RSL

    http://blog.guya.net/2010/09/13/flash-player-10-1-global-error-handling-examples-and-the-flex-3-x-issue/

  7. antonio brandao Says:

    Error #1014: Class flash.events::UncaughtErrorEvent could not be found.

    versions:
    – Adobe AIR 2.0.2 SDK
    – Flex SDK 4.1
    – Flash Player 10, or 10.1

    using Flashdevelop, compiling without IDE

Leave a Reply