Global Exception or Error Handling in Flex
In this post I explain how you can catch unhandled exceptions or errors globally in a declarative mxml-based way using the [Mixin] flex-specific metadata tag.
Starting on FP 10.1 and AIR 2.0 you can capture unhandled exceptions or errors globally. Although you can use these new APIs you should always take care of the exceptions where they happen and use this technique only as a diagnosing or logging help or for async exceptions that you can’t really control in any other way.
You might also be interested in these 2 other topics:
- Logging and catching (async) exceptions during invalidation in Flex
- Debugging and getting Stack-trace error dialogs with installed AIR applications
The new API works like:
private function onUncaughtError(e:UncaughtErrorEvent):void
{
// Do something with your error.
}
Christian Cantrell explains some more details on the APIs. You can find further information in the documentation as well.
When working with Flex, and adding the UncaughtErrorEvent.UNCAUGHT_ERROR listener to the loaderInfo object make sure you do it after FlexEvent.APPLICATION_COMPLETE event has been triggered, otherwise you’ll get an “Error #1009: Cannot access a property or method of a null object reference”
To prevent all this required glue code and avoid unnecessary configuration code (that ends up cluttering the main application Class) I’ve created a GlobalExceptionHandler component that abstracts developers a little bit from these sort of problems. The implementation isn’t tighted to the APPLICATION_COMPLETE event, instead it uses the [Mixin] metadata tag. On top of that you can use it declaratively:
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:logging="com.adobe.ac.logging.*">
<fx:Declarations>
<logging:GlobalExceptionHandler preventDefault="true">
<logging:LogHandlerAction/>
<local:TraceHandlerAction/>
</logging:GlobalExceptionHandler>
</fx:Declarations>
</s:WindowedApplication>
You can download the code along with a sample project here. The code for GlobalExceptionHandler:
{
import flash.display.LoaderInfo;
import flash.events.UncaughtErrorEvent;
import mx.managers.ISystemManager;
[Mixin]
[DefaultProperty("handlerActions")]
public class GlobalExceptionHandler
{
private static var loaderInfo:LoaderInfo;
[ArrayElementType("com.adobe.ac.logging.GlobalExceptionHandlerAction")]
public var handlerActions:Array;
public var preventDefault:Boolean;
public static function init(sm:ISystemManager):void
{
loaderInfo = sm.loaderInfo;
}
public function GlobalExceptionHandler()
{
loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR,
uncaughtErrorHandler);
}
private function uncaughtErrorHandler(event:UncaughtErrorEvent):void
{
for each (var action:GlobalExceptionHandlerAction in handlerActions)
{
action.handle(event.error);
}
if (preventDefault == true)
{
event.preventDefault();
}
}
}
}
The code for LogHandlerAction:
{
import mx.logging.ILogger;
import mx.logging.Log;
public class LogHandlerAction implements GlobalExceptionHandlerAction
{
private static const LOG:ILogger = Log.getLogger("UncaughtException");
public function handle(error:Object):void
{
if (error is Error)
{
var errorObj:Error = error as Error;
LOG.error("{0}. {1}\n {2}",
errorObj.errorID,
errorObj.message,
errorObj.getStackTrace());
}
}
}
}
7 Comments to “Global Exception or Error Handling in Flex”
Leave a Reply

[...] the parameter as an ISystemManager, InteractiveObject or whatever better fits your needs. You can read about another interesting use of the [Mixin] on the Global Exception Handler post [...]
I’m having trouble implementing this. When I launch my apps using adl, everything works fine. But as soon as I launch the installed apps with the regular AIR player or pass -nodebug to adl, catching errors globally does not work at all.
Any hints?
No matter what I do I can’t catch Error 2070.
SecurityError: Error #2070: Security sandbox violation: caller http://stage.domain.com/get_file_content.php?id=31887&inline=true cannot access Stage owned by http://localhost/test-debug/test.swf.
at flash.display::Stage/requireOwnerPermissions()
at flash.display::Stage/addEventListener()
at preloader/frame1()
Hi Judah,
can you share some minimal code that reproduces your issue?
I was getting the Error #2070 when i was loading external swf into my AIR application.After using the Global exception handling I am still getting that error in Debug mode but its no more disturbing me in normal running mode.I am still not able to catch from where this error is coming even using the logging API.
The good thing is application working fine without any #2070 error “cannot access Stage owned by”.
Will the error be trapped if the swf containing this code is run in a non-debugger player?
Yes, the error will be catched but the stack-trace won’t be available