How to catch all exceptions in Flex?

杀马特。学长 韩版系。学妹 提交于 2019-11-26 10:28:02

问题


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.


回答1:


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 = 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
        }
    }



回答2:


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




回答3:


It works in Flex 3.3.

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



回答4:


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




回答5:


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




回答6:


I'm using flex 4. I tried loaderInfo.UncaughtErrorEvents, but loaderInfo was not initialized so it gave me null reference error. Then i tried root.loaderInfo.UncaughtErrorEvents and the same story. I tried sprite.root.UncaughtErrorEvents, but there was no sprite object, I created one, but it didn't work. Finally I tried

systemManager.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR,globalUnCaughtErrorHandler.hanleUnCaughtError);

And guess what, it works like magic. check this




回答7:


It works in Flex 3.5 and flash player 10:

<?xml version="1.0" encoding="utf-8"?>

        protected function application1_addedToStageHandler(event:Event):void{              
            if(loaderInfo.hasOwnProperty("uncaughtErrorEvents")){
                IEventDispatcher(loaderInfo["uncaughtErrorEvents"]).addEventListener("uncaughtError", uncaughtErrorHandler);
            }

            sdk.text = "Flex " + mx_internal::VERSION;
        }

        private function uncaughtErrorHandler(e:*):void{
            e.preventDefault();

            var s:String;

            if (e.error is Error)
            {
                var error:Error = e.error as Error;
                s = "Uncaught Error: " + error.errorID + ", " + error.name + ", " + error.message;
            }
            else
            {
                var errorEvent:ErrorEvent = e.error as ErrorEvent;
                s = "Uncaught ErrorEvent: " + errorEvent.text;
            }

            msg.text = s;
        }

        private function unCaught():void
        {
            var foo:String = null;
            trace(foo.length);
        }
    ]]>
</mx:Script>
<mx:VBox>
    <mx:Label id="sdk" fontSize="18"/>
    <mx:Button y="50" label="UnCaught Error" click="unCaught();" />
    <mx:TextArea id="msg" width="180" height="70"/>
</mx:VBox>

Thanks




回答8:


I attached the event listener to the 'root', which worked for me:

sprite.root.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onUncaughtError);

In the debug Flash Player this will still error, but in the non-debug version the error will appear in Flash Player's dialog box - and then the handler will respond. To stop the dialog box from appearing, add:

event.preventDefault();

so:

    private function onUncaughtError(event:UncaughtErrorEvent):void
    {
        event.preventDefault();
        // do something with this error
    }

I was using this in AIR, but I assume it works for standard AS3 projects too.




回答9:


Now you can, using loader info:

http://www.adobe.com/devnet/flex/articles/global-exception-handling.html

Checkout: loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onUncaughtError);

private function onUncaughtError(e:UncaughtErrorEvent):void
{
    // Do something with your error.
}


来源:https://stackoverflow.com/questions/101532/how-to-catch-all-exceptions-in-flex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!