Dealing with runtime errors in the Flash player

It's a very good thing that uncaught runtime errors are automatically being exposed for users having the debug version of the Flash player plugin. But for websites or applications in production (i.e. "live"), it's a good idea not to scare or annoy users with those standard error dialogs popping up. There are more elegant ways to deal with runtime errors.

In general, you should always prevent runtime errors from bubbling up to the highest level. All pieces of code that can go wrong should be put inside of try-catch blocks. This will already avoid the error from being thrown in the face of your users. Then, it's evenly important (to foresee the possibility) to expose runtime errors again. It's an extremely bad idea to simply let your application fail silently. What I mean is: if you put try-catches everywhere, but you don't expose the error in the catch-block, then you make it impossible to diagnose the problem when your application is acting weirdly.

Systematically logging errors in catch blocks using the Flex logging framework proves its value sooner or later. Use Flex logging in combination with a logging target, like my own XPanelOnAIR panel. When testing your RIA, you can keep the logging panel open in parallel and check out if something went wrong. Also when facing with platform-specific bugs, error logs might come in handy. For example, you have built a rich internet application and a specific user comes across a bug that you can't reproduce because that user's in a specific corporate network, or working on a different platform, or whatever... Ask your user to download & install XPanelOnAIR, perform the necessary actions to invoke the bug, then save the log messages to a log.csv file and finally have him/her send the CSV file by e-mail to you. You can then import the log.csv file in Excel and read what the problem was due to.

So what to do in code exactly? Well, sometimes a few lines of source code say more than a thousand words:

import mx.logging.Log;
import mx.logging.ILogger;
private static const LOGGER:ILogger = Log.getLogger("MyClass");

function whateverFunction():void
{
     try
     {
          doSomethingThatCanGoWrong();
     }
     catch(e:Error)
     {
          LOGGER.error(e.getStackTrace());
     }
}

To know how to make Flex log messages appear in the XPanelOnAIR logging panel, check out these 2 posts:
Flex logging target on AIR
New features in Flex logging panel

This entry was posted in tips. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Trackback

  1. [...] handling Errors abort code execution and that’s a good thing because errors must always be exposed (also read this post about dealing with runtime errors). Things that can go wrong should be put in a try-catch block. Please note that the finally-block [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*