Q:

How to handle the SecurityError #2070 and #2000 in Loader.load()

I have a AIR project that loads a any swf. But when the loaded swf dispatches a SecurityError, I can't handle this error! My code is bellow:
var loader:Loader = new Loader();

_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfComplete);
_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, swfErrors);
_loader.contentLoaderInfo.addEventListener(Event.INIT, swfInit);
_loader.contentLoaderInfo.addEventListener(Event.OPEN, swfOpen);
_loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatus);
_loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, swfSecurityError);
_loader.contentLoaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrors);

try {
    _loader.load(new URLRequest('path.swf'));
}
catch(e:SecurityError) {
    trace("trying handle error!");
}

function swfComplete(e:Event):void {
    trace("complete");
}

function swfErrors(e:IOErrorEvent):void {
    trace("swf error: " + e.toString() );
}

function swfInit(e:Event):void {
    trace("swf init");
}

function swfOpen(e:Event):void {
    trace("swf open. this listener dispatch!");
}

function httpStatus(e:HTTPStatusEvent):void {
    trace("http status: " + e.toString() );
}

function swfSecurityError(e:SecurityErrorEvent):void {
    trace("trying handle security error. :(");
}

function uncaughtErrorEvent(e:UncaughtErrorEvent):void {
    trace("uncaught error: " + e.toString() );
}

The exceptions from FlashDevelop:

[Fault] exception, information=SecurityError: Error #2070: Security sandbox violation: caller file.swf cannot access Stage owned by app:/myApp.swf.
[Fault] exception, information=SecurityError: Error #2000: No active security context.

The problem is that my program quits and I can not catch these security errors. How can I prevent my program from closing?

A:

You code cannot handle this exception because it is not a loading routine violation. It happens when the loaded content tries to access the stage (and does not have right to access it due to sandbox policy).
You can (probably) solve it in several ways:

Load your file as binary data via URLLoader and then Loader.loadBytes it. Thus your content will be taken as internal within the same sandbox and it will have full access to your whole application.
Handle the exception: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/UncaughtErrorEvent.html
Add that SWF as additional file to your app so it is in the same folder as main SWF and will be loaded into the same sandbox too. Cannot say for sure for I never published AIR apps from FD.

