flash - try-catch 不起作用?

标签 flash actionscript-3 error-handling try-catch

我在以下 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/

相关文章:

php - 如何抛出未实现错误

jquery - 查找可点击的世界地图

flash - 是否可以使用 S3 进行 Flash 伪流式传输?

c - 如何从 Flash 调用 C 函数?

flash - 类类型的 AS3 测试

javascript - 在 catch block 中抛出新错误不起作用

javascript - 用 jasmine 单元测试 window.onerror

javascript - Firefox 插件开发并使用 html :embed 在 xul 中嵌入 .swf

flash - 从 Flash 中检测浏览器的类型和版本

java - 我可以使用 .xml < context-param > 作为 .as 类中的常量吗?