Global Error Handler for Flash Player 10.1 not wor

2019-02-15 19:28发布

问题:

Trying to implement the new FP 10.1 Global error handler into my projects but no matter what I do any uncaught error will still show up the Exception window (both in debug and release versions of the SWF). All I want to do is to prevent these popups but instead send a message to my logger. Here's my code ...

EDIT: I simplified the code now. Could somebody do me a favor and test the following class and see if it's works for him? Because it's doesn't for me! ...

package
{
    import flash.display.Sprite;
    import flash.events.UncaughtErrorEvent;    

    public class GlobalErrorHandlerTest extends Sprite
    {
        public function GlobalErrorHandlerTest()
        {
            stage.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onUncaughtError);
            throw new Error();
        }           

        private function onUncaughtError(e:UncaughtErrorEvent):void 
        {
            var message:String;
            if (e.error["message"])
            {
                message = e.error["message"];
            }
            else if (e.error["text"])
            {
                message = e.error["text"];
            }
            else
            {
                message = e.error["toString"]();
            }

            trace("Uncaught Error: " + e.text);
        }
    }
}

回答1:

I had the same issue as above - I was referencing stage.loadInfo, believing that as that references the stage, it would capture all uncaught errors. However, that doesn't work, you have to actually follow grapefukt's suggestion verbatim: On the actual base display object, place the code

loaderInfo.uncaughtErrorEvents.add...

When you try to place onto stage.loaderInfo or frame.loaderInfo, it has no effect.

In my case, I had to place it in the class that extended the base display object. Very Odd.



回答2:

The docs say that:

The UncaughtErrorEvents object that dispatches the event is associated with either a LoaderInfo object or a Loader object.

Thus you must listen to the loaderInfo's uncaughtErrorEvents property of your topmost display object:

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


回答3:

UPDATE: I think I may know why you think this isn't working. I made the mistake of testing inside the debugger. When the debugger stopped on the runtime errors I assumed that this proved my uncaughtErrorHandler function was not working. In fact, this was in error. It's just a quirk of the debugger. The debugger will still stop as if it is an unhandled error or exception, but if you press Run again you'll see it DOES execute the error handling code. See my thread Flex 4.0/4.5 global error handling for more info.

Original response: I am looking for this same info. None of the examples in the API doc nor various blogs on the subject work for me. I've tried just loaderInfo, stage.loaderInfo, systemManager.loaderInfo... It makes no difference. I even tried all of them in a single test case! The addEventListener are being set but the uncaught errors are not firing the uncaughtErrorHandler. Argh. I have wasted far too much time on this! I've reduced it to a very simple program much like above except with all the code in the main mxml file.

How about this: can someone post a Global Error Handling example that DOES work? I'm using Flex SDK 4.1 (I've also tried with 4.5), targeting FP 10.1 (or 10.2 for 4.5), in a mx:Application-based mxml Flex project.

My thread: Flex 4.0/4.5 global error handling



回答4:

You must set up the listener not to a specific view, but to the main stage object, as it's at the top of the display list (thus picking up any exception of any of its children).