Liquidnight // Jörg Birkhold

Direkt zum Inhalt springen

Liquidnight // Jörg Birkhold

Jörg Birkhold about iOS, Objective-C, Android, FlashPlatform, Multitouch, Flex, Air, 3d Interactive and Software Development

Log

Global Exception Handling in Flex

Maybe someone also ran in the situation that your Flex Application is so big that you can’t be 100% sure that there is no uncatched Runtime Error.
This can get quickly ugly and your app will be reacting totally unpredictable. In the current state of the Flash Player there is no global exeption handling.
There is also a good post about that by Doug Mccune.

Using a try catch block everywhere in Flex it is not so easy because a lot of code is executed async by the callLaterDispatcher.
But after looking around a little I found a Variable in UIComponentGlobals.as called catchCallLaterExceptions.
If you set this to true the callLaterDispatcher will run inside a try catch block, and you can set a listener on the systemManager to catch the Error Event.
This is just little tested from my side so I can’t say for sure that it will work always correctly.

UIComponentGlobals.catchCallLaterExceptions = true;
systemManager.addEventListener("callLaterError",globalExeptionCatcher);

The relevant Code is in UiComponent.as

private function callLaterDispatcher(event:Event):void
    {
        // trace(">>calllaterdispatcher " + this);
        UIComponentGlobals.callLaterDispatcherCount++;
 
        // At run-time, callLaterDispatcher2() is called
        // without a surrounding try-catch.
        if (!UIComponentGlobals.catchCallLaterExceptions)
        {
            callLaterDispatcher2(event);
        }
 
        // At design-time, callLaterDispatcher2() is called
        // with a surrounding try-catch.
        else
        {
            try
            {
                callLaterDispatcher2(event);
            }
            catch(e:Error)
            {
                var callLaterErrorEvent:DynamicEvent = new DynamicEvent("callLaterError");
                callLaterErrorEvent.error = e;
                systemManager.dispatchEvent(callLaterErrorEvent);
            }
        }

But this doesn’t work if there is a EventDispatcher involved in the callStack, as  Luke Bayes points out here.
If this is bug in the AVM2? I don’t think so…
But anyway this means it won’t work for events like creationComplete etc.

  1. ziv schrieb am Juni 18, 2009:

    Wow – it sound interesting!

    where did you add the:
    systemManager.addEventListener(“callLaterError”,globalExeptionCatcher);
    ?
    and what kind of exceptions it catch?

    Thanks!

    Ziv

  2. joerg schrieb am Juni 18, 2009:

    It is in my Main Application. But you can add it anywhere where you can get a reference to the systemManager.
    It catches exeptions from actions which are called from the callLaterDispatcher() i.e. updateDisplayList() etc.

    best
    jörg

  3. Global Exception Handling / Crash Reporting in Adobe AIR – Partially Solved? schrieb am September 18, 2009:

    [...] found out about Luke’s post from Jörg Birkhold’s post about his cool discovery that you can easily catch exceptions that are a result of “callLater” functions. [...]

  4. Debabrata schrieb am November 4, 2009:

    Is not it costly to add addEventListener to systemManager?

  5. joerg schrieb am November 4, 2009:

    No I don’t think so. Why should it be costly?

  6. Global Error Handling in Flex | Summa Blog schrieb am Januar 4, 2010:

    [...] to work best is as follows (until a future beta of Flash Player 10 supports this out of the box). Jörg Birkhold describes how to tell the call later dispatcher to throw an event any time an error is [...]

  7. Global Error Handling in Flex | SOA Governance - Service Oriented Architecture - SOA Business - SOA Design - SOA Services - SOA Software - SOA Solutions - SOA Security - SOA Web Service schrieb am Januar 5, 2010:

    [...] to work best is as follows (until a future beta of Flash Player 10 supports this out of the box). Jörg Birkhold describes how to tell the call later dispatcher to throw an event any time an error is [...]

  8. clemo schrieb am September 13, 2011:

    I also use a very similar handler for catching those exceptions but I’m neither able to log these errors nor to show an Alert (or other Popup). For testing I throw an Error in commitProperties. Any ideas?!

  9. joerg schrieb am September 13, 2011:

    It depends which Flex and FlashPlayer version you are using.
    The technique i describe was necessary before the global exception handler in Flash was available.

  10. clemo schrieb am September 13, 2011:

    Minimum FlashPlayer 10.0.22 (Flex 3.5). I don’t think that the build-in global exeption handler solves the problem that I’m not able to log after this kind of Exception. I now found out, that a ExternalInterface call is still possible… So we have to log via javascript…

RSS-Feed für Kommentare dieses Beitrags.

Hinterlasse einen Kommentar

Erlaubte Tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Mehr Lesen

«
»