我在以下 Actionscript 3 代码中使用了 try-catch 块:
try {
this._subtitle = new SubtitleController(subtitlePath, _framerate);
this._subtitle.addEventListener(Event.COMPLETE, subtitleLoaded);
}
catch (e:Error) {
trace('subtitle not found');
}
然后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);
它几乎可以工作,它停止抛出该错误,而是抛出另一个错误。
但是,块的全部意义try-catch不在于做到这一点吗?为什么它不能与 . 一起使用try-catch,但它可以与常规事件侦听器一起使用?