我在以下 Actionscript 3 代码中使用了 try-catch block :
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
block 的全部意义不就是为了做到这一点吗?为什么它不能与 try-catch
一起使用,但它可以与常规事件监听器一起使用?
最佳答案
IOErrors/NetworkErrors 是异步错误。当导致它们的方法被称为正常运行时错误时,它们不会被抛出。否则执行将不得不完全停止,直到(例如)一个文件被完全加载......
关于flash - try-catch 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/704850/