I am loading a swf into another swf using swfloader, I want to catch all the exceptions thrown by the inner swf, is it doable?
问题:
回答1:
Here are some basics that may help. In short, you cannot use try/catch here.
Errors in loading external content cannot be caught with try..catch..finally statements. Instead you have to create event handlers to handle and “catch” the error events. If you do not have an event listener assigned to an error event and that error occurs, the Flash player will inform you of the unhandled error event.
// creating listeners for error events handles
// asynchronous errors
target.addEventListener(ErrorEvent.TYPE, handler);
function handler(event:ErrorEvent):void {
// handle error
}
If you want to invoke your own asynchronous errors, all you need to do is dispatch an event using dispatchEvent that is of the type ErrorEvent. When an unhandled ErrorEvent reaches the Flash player when authoring in Flash, the output window will display the error.
target.dispatchEvent(new ErrorEvent(”type”));
回答2:
As of Flash 10.1 it is now possible to catch all errors thrown by both the main swf and any swfs loaded inside of it.
To do this you need to listen for an UncaughtErrorEvent dispatched from the loaderInfo.uncaughtErrorEvents
object, like so:
loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, handleUncaughtErrors);
function handleUncaughtErrors(e:UncaughtErrorEvent):void
{
e.preventDefault();
}
Please use with caution, as this will suppress all errors from being shown by the debug version of the player, and the flashlog.txt.