Flex 4.0/4.5 global error handling

2019-08-12 16:21发布

问题:

I'm trying to use the new(ish) AS3 global error handling class. I am trying to use it within a Flex mxml application. I can't get it to work at all. Below is an entire program that shows the problem. I set this to use Flash Player 10.2 and compiled with the Flex 4.5 SDK.

I've tried using Flex SDK 4.0 and 4.5 but I get the error in either case. I must be missing something obvious here. This is a normal Flex SWF file that will be shown on a web page. Assuming I could import the UncaughtErrorEvent, I would then do something like this to setup the event handler:

    if(systemManager.loaderInfo.hasOwnProperty("uncaughtErrorEvents")) {                   
        IEventDispatcher(
            systemManager.loaderInfo["uncaughtErrorEvents"]).addEventListener(
               "uncaughtError", uncaughtErrorHandler);
    }

this all seems horribly kludgy but I could live with that except that it doesn't work! I've scoured the web and cannot find any docs or examples that explain how to make this work in my context. Any advice?

Complete program:

    <?xml version="1.0" encoding="utf-8"?>
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" applicationComplete="onApplicationComplete();"
                   >
        <fx:Declarations>
            <!-- Place non-visual elements (e.g., services, value objects) here -->
        </fx:Declarations>
        <fx:Script>
            <![CDATA[
                private function onApplicationComplete() : void {
                    systemManager.loaderInfo.uncaughtErrorEvents.addEventListener("uncaughtError", uncaughtErrorHandler);
                }
                private function uncaughtErrorHandler(event:Event) : void {
                    trace(event.toString());
                }
            ]]>
        </fx:Script>
        <s:Button x="153" y="64" label="Trigger Error" id="triggerButton" click="throw new Error('myError')"/>
    </s:Application>

回答1:

Take from the API (which I recommend you look through next time):

<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/halo"
                       applicationComplete="applicationCompleteHandler();">

    <fx:Script>
        <![CDATA[
            import flash.events.ErrorEvent;
            import flash.events.MouseEvent;
            import flash.events.UncaughtErrorEvent;

            private function applicationCompleteHandler():void
            {
                loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
            }

            private function uncaughtErrorHandler(event:UncaughtErrorEvent):void
            {
                if (event.error is Error)
                {
                    var error:Error = event.error as Error;
                    // do something with the error
                }
                else if (event.error is ErrorEvent)
                {
                    var errorEvent:ErrorEvent = event.error as ErrorEvent;
                    // do something with the error
                }
                else
                {
                    // a non-Error, non-ErrorEvent type was thrown and uncaught
                }
            }

            private function clickHandler(event:MouseEvent):void
            {
                throw new Error("Gak!");
            }
        ]]>
    </fx:Script>

    <s:Button label="Cause Error" click="clickHandler(event);"/>
</s:WindowedApplication>


回答2:

    <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"
                           uncaughtError="uncaughtErrorHandler(event)" />

The easy way...