swfloader: can I catch all the exceptions from the loaded swf?

老子叫甜甜 提交于 2019-12-23 06:01:53

问题


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.



来源:https://stackoverflow.com/questions/2228034/swfloader-can-i-catch-all-the-exceptions-from-the-loaded-swf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!