Global Error handler with Flash Player 10.1

With FlashPlayer 10.1 comes the ability to handle all the exceptions at one place, aka, Global Error Handler. This means a SWF can be made so that it does not pop-up usual error message dialogues, we generally see now a days with a lot many sites, if we have a debug version of the player. This simply does not mean we should ignore any errors in code. What does it mean is, we still have another gate to filter out any error messages from our application. There can be situation in a team environment, where someone can missout something and that will throw an error on runtime. Well, without further digging into the cause, lets dig into our point of handling the errors.
There is basically another property now to the “LoaderInfo” and “Loader” object. This property is known as “uncaughtErrorEvents”. The trick is to listen to “UncaughtErrorEvent.UNCAUGHT_ERROR” event on this property of the “LoaderInfo” and “Loader” object and handle it. Further more, if we completely want to supress the error dialogue box from poping out, “preventDefault()” must be called on the error handler.
Jumping to the code, add an event handler to the keyboard event like
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
On its handler try to remove a sprite from the display list,which is not yet present in the display list. The code is as below
private function onKeyDown(e:KeyboardEvent):void
{
var sp:Sprite = new Sprite();
this.removeChild(sp);
}
At this point, if we try and run our swf and press any key on our keyboard, the error dialogue will come up.(Keep in mind, this only happens if we have a debug version of the FlashPlayer)
Well, now this dialogue is very annoying if we are an user.  We can avoid this by handing the error on our GlobalError handler. In the beginning of the application listen for error on the LoderInfo object of the main application.
this.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, handleGlobalErrors);
Now, on its handler call “preventDefault()” method of the error event.
private function handleGlobalErrors(e:UncaughtErrorEvent):void
{
//this suppresses the error dialogue
e.preventDefault();
//application specific error handlers goes below

}
The next scenario is when we will be loading external SWFs into our main application. In this case just listen to the Loader objects “uncaughtErrorEvents” property for “UncaughtErrorEvent.UNCAUGHT_ERROR” event as below
var l:Loader = new Loader();
l.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, handleGlobalErrors);
l.load(new URLRequest(“myOther.swf”));
Thats all, now we have a Global Error Handler in place.
Note :
1. SWFs made for AVM1 with AS2 or AS1 can not be handled for errors on Global Error handler.
2. Even if the Error is handled in the Global Error Handler, the execution of the script does not continue on the call stack that caused the error.
Happy Flashing :)
Advertisements

3 thoughts on “Global Error handler with Flash Player 10.1

  1. Hey, just a heads up using this technique kills backwards compatability in FP9/10. If you wish to target versions less than 10.1 yet still take advantage of the new global error handling then you can try this method. It’s less elegant, but it works:

    // This technique degrades gracefully in older unsupported players
    if(loaderInfo.hasOwnProperty(“uncaughtErrorEvents”))
    {
    IEventDispatcher(loaderInfo[“uncaughtErrorEvents”]).addEventListener(“uncaughtError”, uncaughtErrorHandler);
    }

    private function uncaughtErrorHandler(e:Event):void { }

  2. saumya, thanks for clearing things up for me – just what i was looking for (sincerely, it was the first page i landed on looking up results on the topic).
    Well written article, straight to the point!

    happy coding!

  3. I think this is among the most vital info for me.
    And i am glad reading your article. But wanna remark on few
    general things, The site style is perfect, the articles
    is really excellent : D. Good job, cheers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s