views:

7183

answers:

6

When I run a Flex application in the debug flash player I get an exception pop up as soon as something unexpected happened. However when a customer uses the application he does not use the debug flash player. In this case he does not get an exception pop up, but he UI is not working.

So for supportability reasons, I would like to catch any exception that can happen anywhere in the Flex UI and present an error message in a Flex internal popup. By using Java I would just encapsulate the whole UI code in a try/catch block, but with MXML applications in Flex I do not know, where I could perform such a general try/catch.

+19  A: 

There is no way to be notified on uncaught exceptions in Flex 3. Adobe are aware of the problem but I don't know if they plan on creating a workaround.

The only solution as it stands is to put try/catch in logical places and make sure you are listening to the ERROR (or FAULT for webservices) event for anything that dispatches them.

Edit: Furthermore, it's actually impossible to catch an error thrown from an event handler. I have logged a bug on the Adobe Bug System.

Update 2010-01-12: Global error handling is now supported in Flash 10.1 and AIR 2.0 (both in beta), and is achieved by subscribing the UNCAUGHT_ERROR event of LoaderInfo.uncaughtErrorEvents. The following code is taken from the code sample on livedocs:

public class UncaughtErrorEventExample extends Sprite
{
    public function UncaughtErrorEventExample()
    {
        loaderInfo.uncaughtErrorEvents.addEventListener(
            UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
    }

    private function uncaughtErrorHandler(event:UncaughtErrorEvent):void
    {
        if (event.error is Error)
        {
            var error:Error = e.error as Error;
            // do something with the error
        }
        else if (event.error is ErrorEvent)
        {
            var errorEvent:ErrorEvent = e.error as ErrorEvent;
            // do something with the error
        }
        else
        {
            // a non-Error, non-ErrorEvent type was thrown and uncaught
        }
    }
Richard Szalay
Does global error handling in Flash 10.1 require working with flex 3.5? 4?Or does it work in Flex 3 as well?
Assaf Lavie
My code above required Flex 4. However, it should work in any SDK running against 10.1 if you use `((IEventDispatcher)loaderInfo["uncaughtErrorEvents"]).addEventListener("uncaughtError", handlerFunction)`, because the properties will exist at runtime in the player. You could even wrap it with `if (loaderInfo.hasProperty("uncaughtErrorEvents") { }` to ensure it doesn't break in Flash 9/10 (the error handling won't work, of course, but it won't crash)
Richard Szalay
@Richard's comment: That would indeed by how you'd expect it to work, but unfortunately, it doesn't. If you compile with Flash Player 9 as target, and run it on Flash Player 10.1, loaderInfo["uncaughtErrorEvents"] is still not available! My interpretation: The Flash player, at runtime, looks at what player your swf was targetted to, and "hides" the features that weren't in that version yet.
Wouter Coekaerts
@Wouter - I also see that behavior. Feel free to vote/add comments to my bug: https://bugs.adobe.com/jira/browse/FB-27199
Richard Szalay
To add to this answer: if you are running in a debug version of the Flash Player, the general runtime error dialog will still pop up. To prevent this, call event.preventDefault() inside your global error handler.
Christophe Herreman
@Wouter - It turns out the bug link I sent you is wrong. See this bug and the associated link: https://bugs.adobe.com/jira/browse/SDK-28018
Richard Szalay
@Richard - thanks for pointing out that rsl issue! I've posted a workaround on SDK-28018. You might want to integrate that in your answer.
Wouter Coekaerts
+5  A: 

There is a bug/feature request for this in the Adobe bug management system. Vote for it if it's important to you.

http://bugs.adobe.com/jira/browse/FP-444

+1  A: 

Note that bug FP-444 (above) links to http://labs.adobe.com/technologies/flashplayer10/features.html#developer that since Oct 2009 shows that this will be possible as of 10.1, which currently, Oct 28, 2009 is still unreleased - so I guess we'll see if that is true when it gets released

Peter V. Mørch
+3  A: 

Thank you very much! It works in Flex 3.3.

 if(loaderInfo.hasOwnProperty("uncaughtErrorEvents")){
    IEventDispatcher(loaderInfo["uncaughtErrorEvents"]).addEventListener("uncaughtError", uncaughtErrorHandler);
 }
Uwe
+1  A: 

Alternative to accepted answer, using try-catch. Slower, but more straightforward to read, I think.

try {
    loaderInfo.uncaughtErrorEvents.addEventListener("uncaughtError", onUncaughtError);
} catch (e:ReferenceError) {
    var spl:Array = Capabilities.version.split(" ");
    var verSpl:Array = spl[1].split(",");

    if (int(verSpl[0]) >= 10 &&
        int(verSpl[1]) >= 1) {
        // This version is 10.1 or greater - we should have been able to listen for uncaught errors...
        d.warn("Unable to listen for uncaught error events, despite flash version: " + Capabilities.version);
    }
}

Of course, you'll need to be using an up-to-date 10.1 playerglobal.swc in order to compile this code successfully: http://labs.adobe.com/downloads/flashplayer10.html

aaaidan
A: 

Can you provide a full example, please? I am not able to intercept uncaught error in my flex application. Thanks

Marco Plebani
You should add questions as comments, not as answer.
Yaba