You Can Catch Timeouts and Stack Overflows in Actionscript 3

While exploring the AS3 Language Reference today, I discovered a couple of potentially useful classes in the flash.errors package, ScriptTimeoutError and StackOverflowError. The package itself contains special classes specifically designed for the Flash Player.

By putting a try-catch block around a potentially long operation, you can catch a ScriptTimeoutError to stop Flash from the usual crash and burn. Now your application can fail gracefully if you need to run some heavy duty calculations. At first, I expected this to be more useful. I could see a Flash movie that would catch the timeout error, update the screen with it’s progress, then continue on with the calculations. Unfortunately, it looks like you’ll only have one opportunity to catch the error. According to the ASLR, the second time your script times out, you’re out of luck.

The StackOverflowError class can be used to catch problems with deep recursion. Like the timeout error, you’ll be able to fail gracefully without the potentially-confusing Flash Player message appearing. Even better, I believe that a clever programmer could use this one to run especially long recursive problems. Unlike the previous error, you don’t get one opportunity. Imagine saving the state of your calculations every time you make a recursive call. When you get a StackOverflowError, you can start over with the saved value and run your operation with a clean stack. As long as you keep saving the state, you may continue until the operation is done. Of course, you’ll have to be extra careful because you may end up receiving the pesky timeout error I described above. It seems to me, however, that you could let your Flash movie rest for a moment after each stack error to refresh itself. You can update a progress bar or something to visually indicate that progress has been made so the user isn’t left wondering if something bad happened.

Obviously, you won’t be using this technique often. Even with the new faster VM, Flash isn’t exactly the best choice for heavy calculation. Internet culture being what it is, you’ll need to consider that the user could get impatient if they have to wait too long. It’s all about balance. Just think of this as another tool that you may never use, but if you ever need it, it’s there, free of charge.

Important Note: I tried running some operations that should give timeout messages and stack overflow errors. For some reason, the movie completely stopped responding, and I needed to force-quit the browser via Windows Task Manager. Even when I tried catching the errors, nothing happened. I’m guessing this hasn’t been fully implemented, or this might be a particular behavior that only appears in a debug player like the one we’re working with now. File this technique away for future use.

About Josh Tynjala

Josh Tynjala is a frontend developer, open source contributor, bowler hat enthusiast, and karaoke addict. You might be familiar with his project, Feathers UI, an open source user interface library for Starling Framework that is included in the Adobe Gaming SDK.

Discussion

  1. Vesa Kortelainen

    Thanks for showing this and I totally agree with your point: “Internet culture being what it is, you’ll need to consider that the user could get impatient if they have to wait too long. It’s all about balance.” Keepin’ things in balance is very important (when programming, when making UIs or even when you are wondering should I play few more hours this Halo2 on XBL or go to sleep 😉