How to catch all exceptions in Flex?

There is no way to be notified on uncaught exceptions in Flex 3. Adobe are aware of the problem but I don’t know if they plan on creating a workaround.

The only solution as it stands is to put try/catch in logical places and make sure you are listening to the ERROR (or FAULT for webservices) event for anything that dispatches them.

Edit: Furthermore, it’s actually impossible to catch an error thrown from an event handler. I have logged a bug on the Adobe Bug System.

Update 2010-01-12: Global error handling is now supported in Flash 10.1 and AIR 2.0 (both in beta), and is achieved by subscribing the UNCAUGHT_ERROR event of LoaderInfo.uncaughtErrorEvents. The following code is taken from the code sample on livedocs:

public class UncaughtErrorEventExample extends Sprite
{
    public function UncaughtErrorEventExample()
    {
        loaderInfo.uncaughtErrorEvents.addEventListener(
            UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
    }

    private function uncaughtErrorHandler(event:UncaughtErrorEvent):void
    {
        if (event.error is Error)
        {
            var error:Error = event.error as Error;
            // do something with the error
        }
        else if (event.error is ErrorEvent)
        {
            var errorEvent:ErrorEvent = event.error as ErrorEvent;
            // do something with the error
        }
        else
        {
            // a non-Error, non-ErrorEvent type was thrown and uncaught
        }
    }

Leave a Comment