evi*_*uin 2 flash error-handling try-catch actionscript-3
我在以下Actionscript 3代码中使用了try-catch块:
try {
this._subtitle = new SubtitleController(subtitlePath, _framerate);
this._subtitle.addEventListener(Event.COMPLETE, subtitleLoaded);
}
catch (e:Error) {
trace('subtitle not found');
}
Run Code Online (Sandbox Code Playgroud)
然后SubtitleController构造函数尝试加载subtitlePath并抛出一个Error #2044: Unhandled ioError,但错误不会被try语句捕获.简单地抛出错误就像没有try声明一样.
当然,我可以用这个代码替换
this._subtitle.addEventListener(IOErrorEvent.IO_ERROR, function (ev:Event) { trace('subtitle not loaded'); });
this._subtitle = new SubtitleController(subtitlePath, _framerate);
this._subtitle.addEventListener(Event.COMPLETE, subtitleLoaded);
Run Code Online (Sandbox Code Playgroud)
它几乎可以工作,它停止了这个错误,但却引发了另一个错误.
但这不是try-catch要做到这一点的重点吗?为什么它不适用于try-catch,但它确实适用于常规事件监听器?
基本上,因为调用是异步的try..catch..finally块不会对你有好处.加载器需要一段时间才能确定url是坏的,然后调度IO_ERROR事件.- http://www.mattmaher.net/flexible_code/index.php/2008/01/10/urlloader-stream-error-handling/
西奥是对的; 我只想添加使用包的IOErrorEvent类flash.events来处理任何不知道该方法的人.
var loader:URLLoader = new URLLoader();
loader.load(new URLRequest("test.mp3"));
loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
function onIOError(e:IOErrorEvent):void
{
trace("An IO Error has occured.\n\n", e);
}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是Loader对象而不是URLLoader,请记住您需要按如下方式侦听Loader对象的contentLoaderInfo属性.
var loader:Loader = new Loader();
addChild(loader);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
loader.load(new URLRequest("path/to/asset"));
function onIOError(e:IOErrorEvent):void
{
trace("An IO Error has occured.\n\n", e);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4294 次 |
| 最近记录: |