310.684.3047

Catch All Unhandled AS3 Errors in FP 10.1

I've been preparing my 'Practical ActionScript 3' talk for MAX 2010. This process consists mainly of updating the slides and code from the same talk at MAX 2009 (archived here on Adobe TV).

Of course, since last year's MAX, we've had a new version of the Flash Player, and the Flash authoring tool. The changes to AS3? Not much. In fact, I'd say that almost nothing has changed for most developers.

There is one new feature of Flash Player 10.1, though, that I wanted to cover. Ever since the introduction of ActionScript 3 with Flash Player 9, many users of Flash have experienced some kind of shock coming to terms with the number of run-time errors that can suddenly start appearing in their code.

Of course, the process of becoming a good ActionScript 3 developer exists in large part with learning how to handle errors (and room for potential errors) in your code, by writing proper event handlers. And, before Flash Player 10.1, all you could really do was hunt down potential errors, one by one, listen for those events, and author handlers to handle them if/when they occur. But with Player 10.1, there is a new class, the UncaughtErrorEvents class (flash.events.UncaughtErrorEvents), that allows you to catch all errors in your movie — permitting you to handle any straggler errors in one location, and ensuring that you can suppress all run time errors.

To do so, either on the main timeline (if you are coding in Flash Professional) or in the Document Class, write the following:

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

This line of code will allow you to hear all unhandled errors (a.k.a., UncaughtErrorEvent instances) in your movie, and react to them in your handleGlobalErrors callback function.

But, this will not actually suppress any run time errors from appearing (for example, if you have the debug version of the Flash Player installed). If you wish to ensure that no run time error dialog boxes appear, you will need to ensure that you preventDefaultBehavior on the UncaughtErrorEvent. You can do so by writing this in your callback function.

function handleGlobalErrors( evt : UncaughtErrorEvent ):void
{
	evt.preventDefault();
}

BE EXTREMELY CAREFUL!

Once you do this, you will (by definition!) not see any more run time errors. So you'd better, at a minimum, have some good tracing going on in your callback function. It is for this reason, that many engineers would say suppressing all run time errors is bad practice — and they may well be right — but you never know which client (or potential client!) has the debug player installed, so you just might want to ensure you place this sort of mechanism on all deployed code.

But wait, it gets better!

Let's say you're a super-duper, on-the-ball, tight-wad engineer who handles each and every one of your errors and potential errors. Let's say you have absolutely no need or desire to implement such a meager global error catching mechanism on your own code.

The thing is: Flash can load Flash. Your Flash might have to load someone else's Flash at some point — after all, a lot of Flash does.

This mechanism allows you to ensure that your Flash won't generate run-time errors, even if it loads poorly-written, error-laden Flash movies. You just listen for UncaughtErrorEvents on the loader that loads the external Flash, instead of on the stage. Like so:

var loader = new Loader ( ) ;
loader.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, handleGlobalErrors);
loader.load ( new URLRequest ( "someTotallyUnsafe.swf" ) ;

Again, as Spider Man's uncle said, 'with great power comes great responsibility' — it can be easy to get yourself into trouble with this; but use the UncaughtErrorEvent wisely, and it will be a powerful ally in your coding.

Share and enjoy!

-r

Category: Code & Samples

Tagged: , ,

4 Responses

  1. [...] This post was mentioned on Twitter by Joseph Burchett and derwin sadiwa, Phlashers. Phlashers said: Catch All Unhandled AS3 Errors in Flash Player 10.1 http://labs.almerblank.com/2010/09/catch-all-unhandled-as3-errors [...]

  2. Cardin says:

    Whoa, thanks for the heads up! I'm not sure how I would be using this, maybe to build a Console logger of sorts via UncaughtErrorEvent.error, but this info is sure to come in useful during program release phase when I HAVE to suppress all errors!

  3. Aditya says:

    Thanks for the post. I can see soo many uses of this right away.Dint realize this existed in fp 10.1. Definitely good way to handle unexpected runtime exceptions.

  4. Siriden says:

    Thanks for the post, really helpful.

Leave a Reply